当前位置 : 主页 > 网络推广 > seo >

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

来源:互联网 收集:自由互联 发布时间:2021-06-16
这篇文章是 How to make ‘access_type=offline’ / server-only OAuth2 operations on GAE/Python的后续内容.http:credentials = credentials.authorize(httplib2.Http())部分在测试时不再失败,但似乎它仍然由GAE的cron运行
这篇文章是 How to make ‘access_type=offline’ / server-only OAuth2 operations on GAE/Python的后续内容.http:credentials = credentials.authorize(httplib2.Http())部分在测试时不再失败,但似乎它仍然由GAE的cron运行,它无法刷新我的access_token:

>我可以通过调用/ fetch手动运行我的工作,比如说在11:45.
>然后在11:55立即调度a / cronfetch作业,然后没有任何access_token问题.
>但是,我今天早上醒来看到相同的/ 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

这个Getting “invalid_grant” error on token refresh邮件列表消息(SO post 1,SO post 2,SO post 3)看起来与我的问题类似,但似乎发生了access_type = online令牌.在我的情况下,我只使用默认的access_type = offline,我在初始访问请求中看到“当我不使用应用程序时执行这些操作”.

我刚刚在08:25重新安排了cron运行(小心不要启动手动运行),调试打印语句是我为你提交给GitHub的.这是我得到的,它是相似但不相同的(请注意,最后几行看起来不正确,我绝对不会在create_playlist中执行OAuth2的东西,直到读取所有源).所以忽略了倾斜的顺序(GAE日志工件?),似乎my http = credentials.authorize(Http()) call in create_playlist(self), currently at line 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

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

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

代码在GitHub,我的实例可以在dailygrooves.org到达.感谢您的帮助!

当刷新令牌不能用于从当前用户获取新的访问令牌时,将返回invalid_grant.发生这种情况是因为存储的Credentials对象具有空刷新令牌,即

>>> credentials.refresh_token is None
True

如How to make ‘access_type=offline’ / server-only OAuth2 operations on GAE/Python?中的注释中所述:

If a user has already authorized your client ID, the subsequent times you perform OAuth for these users they will not see the OAuth dialog and you won’t be given a refresh token.

您需要确保您的凭据存储有有效的刷新令牌,并且最简单的方法是执行此操作,如上一个问题以及您链接的所有3个问题中所述,在创建OAuth2WebServerFlow或OAuth2Decorator时使用approval_prompt = force对象(无论你使用哪个).

网友评论