小编典典

如何在GAE任务队列中执行需要OAuth的操作?

python

我有一个简单的Google App Engine应用程序,其中包含一个/update可更新YouTube播放列表的页面。看起来像这样:

class UpdatePage(webapp2.RequestHandler):

    @decorator.oauth_required
    def get(self):
        update_result = self.update_playlist()
        ...

routes = [('/update', UpdatePage),
          (decorator.callback_path, decorator.callback_handler())]
app = webapp2.WSGIApplication(routes, debug=True)

它可以按预期update_playlist()方式工作,并且该方法可以正常工作,但事实证明,在某些情况下,它可以运行很长时间,结果是DeadlineExceededError。因此,在阅读了可用的选项之后,我发现
Task Queue
API是可行的方法(对吗?),我正按照Python中使用推送队列指南尝试使用它。

→总之,我分裂UpdatePageUpdatePageHandler+ UpdatePageWorker

class UpdateHandlerPage(webapp2.RequestHandler):

    @decorator.oauth_required
    def get(self):
        taskqueue.add(url='/updateworker')

class UpdateWorkerPage(webapp2.RequestHandler):

    def post(self):
        update_result = self.update_playlist()
        ...

routes = [('/update', UpdateHandlerPage),
          ('/updateworker', UpdateWorkerPage),
          (decorator.callback_path, decorator.callback_handler())]
app = webapp2.WSGIApplication(routes, debug=True)

不幸的是,进行拆分后,似乎我的OAuth2装饰器不再执行其工作:

INFO     2013-05-30 17:08:53,971 discovery.py:709] URL being requested: https://www.googleapis.com/youtube/v3/playlists?alt=json&part=snippet%2Cstatus
WARNING  2013-05-30 17:08:53,975 urlfetch_stub.py:480] Stripped prohibited headers from URLFetch request: ['content-length']
INFO     2013-05-30 17:08:54,351 client.py:493] Refreshing due to a 401
INFO     2013-05-30 17:08:54,361 appengine.py:276] make: Got type <class 'google.appengine.api.datastore_types.Blob'>
INFO     2013-05-30 17:08:54,363 appengine.py:289] validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
INFO     2013-05-30 17:08:54,364 client.py:680] Refreshing access_token
INFO     2013-05-30 17:08:54,746 client.py:699] Failed to retrieve access token: {
  "error" : "invalid_grant"
}
INFO     2013-05-30 17:08:54,757 appengine.py:276] make: Got type <class 'google.appengine.api.datastore_types.Blob'>
INFO     2013-05-30 17:08:54,759 appengine.py:289] validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
INFO     2013-05-30 17:08:54,761 appengine.py:289] validate: Got type <class 'oauth2client.client.OAuth2Credentials'>
INFO     2013-05-30 17:08:54,762 appengine.py:265] get: Got type <class 'oauth2client.appengine.CredentialsModel'>

而且,如果不是装饰UpdateHandlerPage:get我装修UpdateWorkerPage:post,我得到一个无限循环的故障,而不是:

INFO     2013-05-30 17:24:31,307 discovery.py:190] URL being requested: https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest?userIp=127.0.0.1
INFO     2013-05-30 17:24:34,960 dev_appserver.py:3105] "GET /update HTTP/1.1" 200 -
INFO     2013-05-30 17:24:35,060 dev_appserver.py:3105] "POST /updateworker HTTP/1.1" 302 -
WARNING  2013-05-30 17:24:35,065 taskqueue_stub.py:1980] Task task1 failed to execute. This task will retry in 0.100 seconds
INFO     2013-05-30 17:24:35,240 dev_appserver.py:3105] "POST /updateworker HTTP/1.1" 302 -
WARNING  2013-05-30 17:24:35,245 taskqueue_stub.py:1980] Task task1 failed to execute. This task will retry in 0.200 seconds
...

我能做什么?谢谢你的帮助!


阅读 200

收藏
2021-01-20

共1个答案

小编典典

由于“任务队列”任务将由您的应用程序生成,因此原始请求中的任何标头都不会通过发送。特别是,Cookies标头通过SACSID应用程序的Cookie标识您的用户(由App
Engine用户API提供)。

更新:(在原始帖子之后添加了此内容。)* 由于没有cookie,因此SACSID标识用户的cookie不会在那里,因此, 每次
cron作业运行时,该decorator.oauth_required名称将强制进行重定向(即HTTP 302)。
*

您最好不要将App Engine用户ID传递给任务,而不是尝试从装饰器获取当前用户。首先获取当前用户(在您的修饰方法内):

from google.appengine.api import users
# Guaranteed not to be None by the decorator
current_user = users.get_current_user()

然后在任务中传递App Engine用户ID

import urllib
query_string = urllib.urlencode({'user_id': current_user.user_id()})
taskqueue.add(url='/updateworker?' + query_string)

然后在您的任务中,您可以检索到 user_id

# This is the 'user_id' you appended in the query string
user_id = self.request.get('user_id')

像装饰器中一样使用它来获取该用户的凭据:

from oauth2client.appengine import CredentialsModel
from oauth2client.appengine import StorageByKeyName
# This assumes you are using the defaults for OAuth2Decorator,
# which your above code is
credentials = StorageByKeyName(
    CredentialsModel, user_id, 'credentials').get()
2021-01-20