Google OAuth2 - isAccessTokenExpired()始终为true

Kro*_*oll 5 php google-oauth

我在我的应用程序中使用OAuth,并且我希望在访问令牌过期时注销用户.

但是当我检查令牌到期时

 $client->isAccessTokenExpired()
Run Code Online (Sandbox Code Playgroud)

它总是返回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();
}
Run Code Online (Sandbox Code Playgroud)

小智 10

您可能在运行之前检查过期时间$client->setAccessToken();.让我们看一下您检查过期的代码.

  • 实际上,当我在 $client->setAccessToken(); 之后运行 $client->isAccessTokenExpired(); 它返回“假”。谢谢! (2认同)