1

I'm using the Gmail API for reads emails and just add/remove labels.

I authenticated correctly, I get my access token with 3600 seconds of life.

And here starts the problems... When I try to Refresh Token, I got a null, so I can't refresh it.

Some code:

 $this->client = new Client();
 $this->client->setApplicationName('App');
 $this->client->setClientId('xxxxxxx');
 $this->client->setClientSecret('xxxxxxx');
 $this->client->setRedirectUri('xxxxxxx/oauth/gmail/callback');
 $this->client->setScopes([GoogleGmail::GMAIL_READONLY, GoogleGmail::GMAIL_MODIFY]);
 $this->client->setPrompt('select_account consent');
 $this->client->setAccessType('offline');

In the callback I do:

$this->client->fetchAccessTokenWithAuthCode($code);

Output:

{
"access_token":"xxxxxxxxxx",
"expires_in":3599,
"scope":"https:\/\/www.googleapis.com\/auth\/gmail.readonly https:\/\/www.googleapis.com\/auth\/gmail.modify",
"token_type":"Bearer",
"created":7374384324 
}

I saved the Access Token, Expired time, and all data really. But I don't get the refresh token.

I try with:

$this->client->getRefreshToken(); // null

And If I do

$this->client->isAccessTokenExpired()

I always get true, inclusive if I auth 5 seconds ago.

I'm using google/apiclient v2.15.0, PHP 8.1

Thanks

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
carlos
  • 140
  • 1
  • 12
  • Please include the outputs of the above codes, so that we can better help you debug your code, especially the one of `fetchAccessTokenWithAuthCode()` call. – msrumon Aug 09 '23 at 03:28
  • I updated with that output. The refresh es null and isAccessTokenExpired is always true as I said – carlos Aug 09 '23 at 03:30
  • Does this answer your question? [Google api refresh\_token null and how to refresh access token](https://stackoverflow.com/questions/38467374/google-api-refresh-token-null-and-how-to-refresh-access-token) – nayeemdev Aug 09 '23 at 03:55
  • Did you use `fetchAccessTokenWithRefreshToken()` anywhere in the code? – msrumon Aug 09 '23 at 03:59
  • @msrumon just in the callback – carlos Aug 09 '23 at 04:02
  • @nayeemdev I'm trying every case. Let me check in a few hours to see when the token expired. – carlos Aug 09 '23 at 04:24
  • 1
    Can you update your question with a minimal setup to reproduce your error? I'd like to try it on my end. – msrumon Aug 09 '23 at 04:31

1 Answers1

1

You only get a refresh token with the first time the user has authenticated your application. You need to store that in your application along with the users name so that you can then send that when you would like to request a new access token.

A refresh of an access token in a web app will not return a new refresh token. Requesting authorization again will also not result in a new refresh token.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449