我在我的应用程序中使用OAuth,当访问令牌过期时,我希望注销用户。
但是当我检查令牌到期时
$client->isAccessTokenExpired()
它总是返回1。
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (!isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
$service = new Google_Service_Calendar($client);
$oauth2 = new Google_Service_Oauth2($client);
$userinfo = $oauth2->userinfo->get();
$emailUser = $userinfo->getEmail();
$_SESSION['emailUser'] = $userinfo->getEmail();
}
发布于 2015-04-25 22:16:49
您可能在运行$client->setAccessToken();
之前检查过期。让我们看看您正在检查过期的代码。
发布于 2022-09-18 13:19:04
在检查isAccessTokenExpired之前,需要设置刷新令牌
发布于 2022-11-14 08:53:46
我知道这是非常古老的,但是函数isAccessTokenExpired()
在通过setAccessToken($params)
设置令牌后排除了expires_in
键时将返回true。
例如,设置访问令牌应该如下,
$client->setAccessToken([
'id_token' => '...',
'created' => '...', // or, if you don't have id_token
'expires_in' => '...',
]);
在谷歌的github中,这是检查expires_in
键的代码:https://github.com/googleapis/google-api-php-client/blob/main/src/Client.php#L570-L573
if (!isset($this->token['expires_in'])) {
// if the token does not have an "expires_in", then it's considered expired
return true;
}
https://stackoverflow.com/questions/29798019
复制相似问题