Google gmail api oauth2 authentication miss understood

Viewed 200

Im trying to create a script for fetching specific emails send in my gmail account.
I created the project at google cloud platform, i followed the instructions from this yt video: https://www.youtube.com/watch?v=j_ilbyXwIzI and it worked. After two or three months back i wanted to try it again because i left it as a side project for a while and it didn't worked.
It throws this error:

**PHP Fatal error: Uncaught Google\Service\Exception: {**
**"error": "invalid_grant",**
**"error_description": "Bad Request"**
**} in /root/vendor/google/apiclient/src/Http/REST.php:128**
**Stack trace:**
**#0 /root/vendor/google/apiclient/src/Http/REST.php(103):**
**Google\Http\REST::decodeHttpResponse()**
**#1 [internal function]: Google\Http\REST::doExecute()**
**#2 /root/vendor/google/apiclient/src/Task/Runner.php(182): call_user_func_array()**
**#3 /root/vendor/google/apiclient/src/Http/REST.php(66): Google\Task\Runner->run()**
**#4 /root/vendor/google/apiclient/src/Client.php(898): Google\Http\REST::execute()**
**#5 /root/vendor/google/apiclient/src/Service/Resource.php(238): Google\Client->execute()**
**#6 /root/vendor/google/apiclient-services/src/Google/Service/Gmail/Resource/UsersMessages.php(168): Google\Service\Resource->call()**
**#7 /root/quickstart.php(72): Google_Service_Gmail_Resource_UsersMessages->listUsersMessages()**
**#8 {main}**

thrown in /root/vendor/google/apiclient/src/Http/REST.php on line 128


After reading in the documentation and google posts without finding the answer, I created a new OAuth2.0 Client ID and configured it for a new web server and it worked as before.

Now my question is, what made the first code not work anymore, what expired? The token or something else. What should I do to prevent from happening this issue again?

Thanks in advance!!

1 Answers

Invalid grant normally means that your refresh token has expired.

The most common cause for that now is that your application is set to testing and not production.

enter image description here

Applications in testing phase will have their refresh tokens reset in seven days. If you want it to last longer then seven days you need to set the application to production.

Related