Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google API PHP Refresh Token returns NULL

I'm using the Google APIs Client Library for PHP for offline authentication, but when I use this code:

$this->google->refreshToken($result["google_refresh_token"]));

it returns NULL.

$this->google refers to an instance of the Google_Client class.

Anyone an idea what this could be?

If I try to change the given refresh token it returns a Google Exception Error, so it should be a valid token.

Thank you!

Edit:

'client_id' => 'myclientid',
      'client_secret' => 'myclientsecret',
      'redirect_uri' => 'mypage',
      'developer_key' => 'mydeveloperkey',

      // Other parameters.
      'access_type' => 'offline',
      'approval_prompt' => 'force',

Maybe this helps you

like image 716
hasanarsa Avatar asked May 01 '14 16:05

hasanarsa


2 Answers

I was having this same issue for quite a while. For me it was because, for some reason, Google only sends you the refresh token the first time you request the token. So you have to make sure you store that refresh token for that user for all of entirety because Google wont give it to you again.

For development this can be a real pain. You can get Google to send you a new refresh token but what you have to do is deauthorize whatever account you authorized through your application. You can do this in your Google security settings.

Hope this helps!

like image 65
ChrisB Avatar answered Sep 19 '22 20:09

ChrisB


I now use a really simple method to avoid the refresh_token() method. I just save the whole JSON response from the first request and give this to the setAccesToken() method. If the access token expires, it will automatically get a new one from the given JSON.

like image 27
hasanarsa Avatar answered Sep 16 '22 20:09

hasanarsa