phpgoogle-oauth

Google OAuth2 - isAccessTokenExpired() always true


I use OAuth in my application and I want log out the user when the access token is expired.

But when I checked the token expiration with

 $client->isAccessTokenExpired()

It always return 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();
}

Solution

  • You probably checking the expiration before running $client->setAccessToken();. Let us see the code where you are checking the expiration.