Google API 凭据刷新失败

Art*_*Art 2 python credentials google-api google-drive-api

这是我的代码的一部分:

 if os.path.exists('token.pickle'):
                with open('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())
Run Code Online (Sandbox Code Playgroud)

如果信用证过期,则必须刷新。在 Windows 上这部分工作,但在 Linux 上我得到一个错误(在最后一个字符串上):

('invalid_scope: Some requested scopes were invalid. {invalid=[a, c, d, e, g, h, i, l, m, ., /, o, p, r, s, t, u, v, w, :]}', '{\n  "error": "invalid_scope",\n  "error_description": "Some requested scopes were invalid. {invalid\\u003d[a, c, d, e, g, h, i, l, m, ., /, o, p, r, s, t, u, v, w, :]}",\n  "error_uri": "http://code.google.com/apis/accounts/docs/OAuth2.html"\n}')
Run Code Online (Sandbox Code Playgroud)

Hon*_* K. 9

我是 python 初学者,我遇到了类似的问题。我编辑了https://developers.google.com/drive/api/v3/quickstart/python以便能够创建事件。我将范围更改为:

SCOPES = 'https://www.googleapis.com/auth/calendar'
Run Code Online (Sandbox Code Playgroud)

起初,我的应用程序运行良好,但后来很奇怪。第二天,它崩溃了,我必须删除令牌才能再次运行该应用程序。当我运行它时,我必须再次允许访问(通过打开的浏览器)。然后它运行良好,但第二天又不行;)

问题是令牌在 3600 秒后过期。这就是奇怪行为的原因。当令牌过期时,它会运行用于刷新现有令牌 ( creds.refresh(Request())) 的代码,但在 Windows 上我收到一个错误:

RefreshError('invalid_scope: Some requested scopes were invalid. {invalid=[a, c, d, e, g, h, i, l, m, ., n, /, o, p, r, s, t, u, w, :]}', '{\n  "error": "invalid_scope",\n  "error_description": "Some requested scopes were invalid. {invalid\\u003d[a, c, d, e, g, h, i, l, m, ., n, /, o, p, r, s, t, u, w, :]}",\n  "error_uri": "http://code.google.com/apis/accounts/docs/OAuth2.html"\n}'),)
Run Code Online (Sandbox Code Playgroud)

和应用程序崩溃。从错误消息我很困惑。

当您删除 token.pickle 时,它​​需要再次通过浏览器进行授权,然后该应用程序可以再运行 3600 秒;) 很多小时我都在寻找问题出在哪里。

最后我找到了。它应该是:

SCOPES = ['https://www.googleapis.com/auth/calendar']
Run Code Online (Sandbox Code Playgroud)

缺少[]括号!!!它导致了上述错误。

现在它对我有用。

提示:当您想测试您的应用程序是否过期和令牌刷新工作时,您可以简单地将系统时钟更改为某个未来日期。