Google-api-php Refresh Token returns invalid_grant

冷暖自知 提交于 2019-11-30 09:16:11

Before Authenticate, there must be something like:

$client->grantType("refresh_token")

You'll get an "invalid_grant" error if you try to refresh when the token isn't expired.

Instead of this:

$client->refreshToken(file_get_contents('../_config/refreshtoken.conf'));

Use this:

$client->setAccessToken(file_get_contents('../_config/refreshtoken.conf'));

Once your token expires you refresh should work.

The invalid_grant means either means that the authorization code has already been used (available in $GET['code']) or the type of application configured in the Google APIs Console is invalid.

Make sure you select "Web Application" when registering your app in the Google APIs Console.

The function that worked for me is as follows

$client->setAccessType("refresh_token");

plowman

I ran into something similar and the problem for me was my system clock (inside the Docker VM where I was running the code) was not synchronized with the real time. So you are requesting a token with a created date too far in the past or future, which OAuth is rejecting.

I was tipped of by the report here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!