如何避免在离线GAE cron任务中"无法检索访问令牌:{"错误":"invalid_grant"}'?

Ron*_*het 5 python google-app-engine google-api oauth-2.0 google-api-python-client

这篇文章是如何在GAE/Python上进行'access_type = offline'/仅服务器OAuth2操作的后续内容.http = credentials.authorize(httplib2.Http())测试时该部件不再失败,但似乎它仍然由GAE的cron运行,它无法刷新我的access_token:

  1. 我可以通过电话手动完成我的工作/fetch,比如说在11:45.
  2. 立即安排/cronfetch在11:55工作,然后没有任何access_token问题.
  3. 但是,我今天早上醒来看到同样的 /cronfetch任务(除了时间,即我的非测试日常任务的01:00时)失败了:

    I 2013-06-10 05:53:51.324 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
    I 2013-06-10 05:53:51.325 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
    I 2013-06-10 05:53:51.327 URL being requested: https://www.googleapis.com/youtube/v3/playlists?alt=json&part=snippet%2Cstatus
    I 2013-06-10 05:53:51.397 Refreshing due to a 401
    I 2013-06-10 05:53:51.420 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
    I 2013-06-10 05:53:51.421 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
    I 2013-06-10 05:53:51.421 Refreshing access_token
    I 2013-06-10 05:53:51.458 Failed to retrieve access token: { "error" : "invalid_grant" }
    I 2013-06-10 05:53:51.468 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
    I 2013-06-10 05:53:51.468 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
    I 2013-06-10 05:53:51.471 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
    I 2013-06-10 05:53:51.471 get: Got type <class 'oauth2client.appengine.CredentialsModel'>
    E 2013-06-10 05:53:51.480 invalid_grant Traceback (most recent call last): File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in
    
    Run Code Online (Sandbox Code Playgroud)

令牌刷新获得"invalid_grant"错误的邮件列表消息(+ SO发布1,SO 2后,SO发布3)类似于我的问题,但它似乎有一个是发生access_type=online令牌.在我的情况下,我只使用默认值access_type=offline,我在初始访问请求中看到"当我不使用应用程序时执行这些操作".

我刚刚在08:25重新安排了cron运行(小心不要启动手动运行),调试打印语句是我为你提交给GitHub的.这是我得到的,它是相似的但不完全相同(请注意,最后几行似乎排序不正确,我绝对不会在create_playlist读取所有来源之前进行OAuth2的操作).所以忽略了倾斜的顺序(GAE日志工件?),似乎我的http = credentials.authorize(Http())调用create_playlist(self),目前在第144行是错误的:

    ...
    E 2013-06-10 08:26:12.817 http://www.onedayonemusic.com/page/2/ : found embeds ['80wWl_s-HuQ', 'kb1Nu75l1vA', 'kb1Nu75l1vA', 'RTWcNRQtkwE', 'RTWcNRQtkwE', 'ZtDXezAhes8', 'ZtDXezAhes8', 'cFGxNJhKK9c', 'cFGxNJhKK9c'
    I 2013-06-10 08:26:14.019 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
    I 2013-06-10 08:26:14.020 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
    I 2013-06-10 08:26:14.022 URL being requested: https://www.googleapis.com/youtube/v3/playlists?alt=json&part=snippet%2Cstatus
    I 2013-06-10 08:26:14.100 Refreshing due to a 401
    I 2013-06-10 08:26:14.105 make: Got type <class 'google.appengine.api.datastore_types.Blob'>
    I 2013-06-10 08:26:14.106 validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
    I 2013-06-10 08:26:14.106 Refreshing access_token
    E 2013-06-10 08:26:18.994 Deadline exceeded while waiting for HTTP response from URL: https://accounts.google.com/o/oauth2/token Traceback (most recent call last): File "/pyt
    E 2013-06-10 08:26:18.996 http://www.onedayonemusic.com/page/3/ : found embeds ['80wWl_s-HuQ', '6VNu2MLdE0c', '6VNu2MLdE0c', 'YwQilKbK9Mk', 'YwQilKbK9Mk', 'KYdB3rectmc', 'KYdB3
    E 2013-06-10 08:26:18.996 crawl_videos end
    E 2013-06-10 08:26:18.996 create_playlist start
    E 2013-06-10 08:26:18.996 create_playlist got creds
    E 2013-06-10 08:26:18.996 create_playlist authorized creds
Run Code Online (Sandbox Code Playgroud)

→为什么cron作业在手动运行后5分钟工作,但在6小时后失效?我以为刷新令牌永不过期.我究竟做错了什么?

请注意,这是我的第一个GAE工作,我的第二个Python程序,一般代码审查/建议非常受欢迎,但请温柔:)

代码在GitHub上,我的实例可以在dailygrooves.org上找到.谢谢你的帮助!

bos*_*ter 4

invalid_grant当刷新令牌无法用于从当前用户获取新的访问令牌时,将返回An 。这种情况发生在您身上,因为存储的Credentials对象具有空刷新令牌,即

>>> credentials.refresh_token is None
True
Run Code Online (Sandbox Code Playgroud)

如何在 GAE/Python 上进行“access_type=offline”/仅服务器 OAuth2 操作?:

如果用户已经授权您的客户端 ID,则您随后为这些用户执行 OAuth 时,他们将看不到 OAuth 对话框,并且您不会获得刷新令牌。

您需要确保您Credentials存储了有效的刷新令牌,并且最简单的方法是执行此操作,正如您的上一个问题以及您链接到的所有 3 个问题中提到的那样,是在approval_prompt=force创建您的OAuth2WebServerFlowOAuth2Decorator对象时使用(无论您使用的是)。