oauth 2.0 - Google drive php API授权失败,无效授权


oauth 2.0 - Google drive php API authorization fails with invalid grant

我在PHP中使用google drive API 。首先,我请求一个Auth url并将用户发送到该url。用户授权并被重定向到我的url,并按照预期在查询字符串中使用授权代码。接下来,调用$client->authorize()并传入查询字符串中的代码。我得到以下错误:

Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant'' in
/home/a5874817/public_html/custom/google/src/auth/Google_OAuth2.php:113
Stack trace:
#0 /home/a5874817/public_html/custom/google/src/Google_Client.php(131): Google_OAuth2->authenticate(Array, '4/jN5JfD_ejozBj...')
#1 /home/a5874817/public_html/custom/docs_reply.php(10): Google_Client->authenticate('4/jN5JfD_ejozBj...')
#2 {main}
thrown in /home/a5874817/public_html/custom/google/src/auth/Google_OAuth2.php on line 113

此帐户之前已授权此应用程序,但auth url始终包含access_type=online,并且它是一个多小时前的,因此访问令牌应该已经过期。造成这个错误的具体原因是什么?

由于您只有在线访问权限,因此当新的访问令牌用完时,您将无法刷新它。

如果您想要永久访问,请请求access_type=offline,并确保您存储了刷新令牌以供以后使用。

嗯,问题已经解决了,但我不记得我所做的许多更改中究竟是哪一个解决了它。我认为问题是我的auth url中有错误的access_type类型,或者客户端设置错误。如果其他人有类似的问题,我建议确保作用域是正确的,并实际阅读所有的auth url,以确保它是有意义的。