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

Python 如何在GAE任务队列中执行OAuth要求的操作?,python,google-app-engine,google-api,task-queue,google-api-python-client,Python,Google App Engine,Google Api,Task Queue,Google Api Python Client,我有一个简单的Google App Engine应用程序,其中包括一个更新YouTube播放列表的/update页面。看起来是这样的: class UpdatePage(webapp2.RequestHandler): @decorator.oauth_required def get(self): update_result = self.update_playlist() ... routes = [('/update', UpdatePa

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

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\u playlist()
方法完成了它的工作,但在某些情况下,它可能会运行相当长的时间,从而导致
死线超出错误。因此,在阅读了有关可用选项的内容后,我认为任务队列API是一个不错的选择(对吗?),我正按照指南尝试使用它

→ 简而言之,我将
UpdatePage
拆分为
UpdatePage处理程序
+
UpdatePage工作人员

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'>

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

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

更新:(这是在原始帖子之后添加的。)由于没有cookie,识别用户的
SACSID
cookie将不在那里,因此
装饰器。oauth_required
指定将在cron作业运行时强制重定向(即HTTP
302

与其尝试从decorator获取当前用户,不如将appengine用户ID传递给任务。首先获取当前用户(在您的方法中):

然后在任务中传递应用程序引擎用户ID

import urllib
query_string = urllib.urlencode({'user_id': current_user.user_id()})
taskqueue.add(url='/updateworker?' + query_string)
然后在您的任务中,您可以获取该
user\u id

# This is the 'user_id' you appended in the query string
user_id = self.request.get('user_id')
并使用它获取该用户的凭据:


成功了,再次感谢!这为一个新问题打开了大门:手动打开
/update
工作,但现在我想安排一个cron任务,每晚查询它。我想这意味着使用
用户。获取当前用户()
是不可能的,我必须缓存它?首先,你应该在你的问题中提出这个问题,或者问一个新的问题。如果希望cron作业以特定用户的身份运行,可以将用户id硬编码到脚本中。如果要为所有用户运行该程序,可以从
CredentialsModel
查询所有存储的凭据,然后为每个用户生成一个任务。是的,抱歉。我已经问了很多问题,所以我想我应该把这个问题放在这里,这确实是个坏主意。我试过你的建议,但我的cron任务总是失败;接下来我要问一个适当的新问题:
import urllib
query_string = urllib.urlencode({'user_id': current_user.user_id()})
taskqueue.add(url='/updateworker?' + query_string)
# 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()