0

To start I am using the Google OAuth 2.0 code from this site https://github.com/google/google-api-php-client

I need to find out where in this oauth directory the token expires and logs you out. I am having issues with the refresh token and usually the token expires in 1 hour and throws me an error, but I cant keep waiting for 1 hour each time I make a change to see if the code works or not. I have changed some time settings in the code to like 10 or 60 seconds but they don't do anything. Please let me know which file and where I can change the time the token expires and logs out the logged in user.

Thanks,

I have added the following code because the problem is in here, something with this get function is not renewing/using the refresh token. How can I write this code better.

    $service = new Google_Service_Oauth2 ($client);

    if ($client->getAccessToken()) {
        //For logged in user, get details from google using access token
        $user = $service->userinfo->get();
        $user_id = filter_var($user['id'],FILTER_SANITIZE_SPECIAL_CHARS);
        $user_name = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
        $first_name = filter_var($user['given_name'], FILTER_SANITIZE_SPECIAL_CHARS);
        $last_name = filter_var($user['family_name'], FILTER_SANITIZE_SPECIAL_CHARS);
        $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
        // $profile_url = filter_var($user['link'], FILTER_VALIDATE_URL);
        $profile_image_url = filter_var($user['picture'], FILTER_VALIDATE_URL);
        $gender = filter_var($user['gender'], FILTER_SANITIZE_SPECIAL_CHARS);
        // $personMarkup = "$email<div><img src='$profile_image_url?sz=50'</div>";
        $_SESSION['upload_token'] = $client->getAccessToken();
    }
John
  • 465
  • 5
  • 15
jm874327
  • 158
  • 4
  • 13

1 Answers1

1

There is no way to change Google's access token expiry time. However, the Google_Client::isAccessTokenExpired() method will return true if the token has expired or expires in 30 seconds from now. Your code should not need to deal with renewing a token only after it fails but can check if the access token is expired before it is going to call any method with that particular access token.

There's still an edge case that remains: you can simulate that by manually revoking the access token (out-of-band of your app) using:

curl https://accounts.google.com/o/oauth2/revoke?token=<access_token>

and then run/test your code that still holds on to the now revoked access token. The error code on access is the same for revoked or expired ("invalid_token"), and the handling is the same anyhow.

Hans Z.
  • 50,496
  • 12
  • 102
  • 115