RefreshError (‘invalid_grant: Token has been expired or revoked.’) Google API

huangapple go评论88阅读模式
英文:

RefreshError ('invalid_grant: Token has been expired or revoked.') Google API

问题

大约一周前,我在Google上设置了一个应用程序。现在当我尝试运行以下代码时:

SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
creds = None
if os.path.exists('token.pickle'):
     with open(self.CREDENTIALS_PATH+self.conjoiner+'token.pickle', 'rb') as token:
        creds = pickle.load(token)
        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request()) ##错误在这里

我得到了以下错误:

Exception has occurred: RefreshError
('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})

可能的问题是什么?

英文:

About a week ago I set up an application on google. Now when I tri and run:

SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
creds = None
if os.path.exists('token.pickle'):
     with open(self.CREDENTIALS_PATH+self.conjoiner+'token.pickle', 'rb') as token:
        creds = pickle.load(token)
        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request()) ##error here

I get the following error:

Exception has occurred: RefreshError
('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})

What could be the problem?

答案1

得分: 1

token.pickle 文件包含了用于您的应用程序的访问令牌和刷新令牌。

"Token has been expired or revoked." 意味着此文件中的刷新令牌不再有效,这可能由以下几个原因引起:

  1. 用户撤销了您的访问权限。
  2. 用户已经授权了您的访问令牌超过 50 次,而这是最旧的令牌,因此已经过期。
  3. 刷新令牌在六个月内没有被使用。
  4. 项目仍处于测试阶段,因此刷新令牌在七天后将过期。

所有上述信息都可以在 Oauth2 过期文档 中找到。

解决选项 1-3 的方法是简单地删除 token.pickle 文件,然后再次请求用户的授权。

对于第四个选项,您应该转到 Google 开发者控制台的 OAuth2 同意屏幕下,并将您的应用程序设置为生产模式。然后您的刷新令牌将不再过期。然后,您可以删除 token.pickle 文件,就不会再遇到此问题。

RefreshError (‘invalid_grant: Token has been expired or revoked.’) Google API

英文:

token.pickle contains the access token and refresh token for your application.

>Token has been expired or revoked.'

Means that the refersh token in this file is no longer working this can be caused by servral reasons.

  1. the user revoked your access
  2. The user has authorized your access token more then 50 times and this is the oldest token and was there for expired
  3. the refresh token hasn't been used in six months
  4. The project is still in the testing phase, and there for the refresh token will be expired after seven days.

All of the above information can be found in Oauth2 expirationdocumentation

the solution for options 1- 3 is to simply delete the token.pickle file and request authorization of the user again.

For number four you should go to google developer console under the Oauth2 consent screen and set your application to production. Then your refresh token will stop expiring. Then you can delete the token.pickle and you wont have this issue again.

RefreshError (‘invalid_grant: Token has been expired or revoked.’) Google API

答案2

得分: 0

没有一个答案在这里或这里 完全帮助我回到正轨,但解决方法非常简单(且晦涩):

  • 删除你的Google令牌.pickle文件,然后重新运行你的脚本。

在我的情况下,我正在使用一个名为GoogleSheets的Julia封装库来操作Google的Sheets API。在这种情况下,这两个文件以google_sheets_token开头,并存储在~/.julia/config/google_sheets/中,同时还有credentials.json文件。

我不知道为什么会有两个pickle文件(也许是为了两个授权范围,尽管老实说,Google的许多身份验证方案对我来说都很神秘),但删除它们两者都会导致下一次运行脚本时在浏览器窗口中弹出OAuth提示,一切都恢复正常。

它们似乎经常过期,关于为什么会过期有各种自信的理论。但是原因似乎会随着它们的过期而变化,因此每次只需删除它们似乎是最可靠的修复方法。

英文:

None of the answers here or there quite got me back on track, but the resolution was quite simple (and obscure):

  • Delete your google token .pickle files and re-run your script.

In my case I'm using a Julia wrapper around Google's Sheets API called GoogleSheets. The (two) files in that case start with google_sheets_token and are stored in ~/.julia/config/google_sheets/ along with the credentials.json file.

I don't know why there were two pickle files (maybe for two auth scopes, though to be fair much of Google's authentication schemes are mysterious to me) but deleting both of them causes the next run of the script to throw up the OAuth prompt in a browser window, and all is well again.

They seem to expire fairly often and there are various confident theories about why. But the reason seems to change roughly as often as they expire so just deleting them each time seems to be the most reliable fix.

huangapple
  • 本文由 发表于 2023年1月9日 10:07:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定