1

I want to get users info after they login with Google Oauth 2.0. But I keep getting errors asking for refresh tokens after 1 hour. I dont want to use the refresh tokens, as I have set access type for online and approval prompt to auto.

This is the code that is causing issues, in particular I get errors with the get() function.

$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      = $user['id'];
          $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['token']    = $client->getAccessToken();
    }

1 Answer 1

1

Refresh the access token, if necessary.

Access tokens have limited lifetimes (One hour to be exact). If your application needs access to a Google API beyond the lifetime of a single access token, you will need to use your refresh token to get a new access token.

A refresh token allows your application to obtain new access tokens.

To answer your question if you want to access your users data for more then one hour it is not possible to do this with out using the refresh token to get a new access token.

Sign up to request clarification or add additional context in comments.

7 Comments

I dont need to have access to the data, as I am saving it in my database as soon as they log in for the first time. But I am assuming I can't be storing their info unless I have the offline access? Is there a way to get around this?
All offline access does is return a refreshToken to you so you can use it without having to request access from the user again.
My end goal is to not have the user keep giving permission for offline access and save their info with the above code. Is this possible without offline access?
Save the refresh token you will be able to get a new access token to access the data without asking them again. I think you should read the rfc for Oauth I think you are misunderstanding something some how
So you're saying once the refresh token has been used, next time the user logs in it wont ask for offline access?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.