【问题标题】:How to do OAuth-requiring operations in a GAE Task Queue?如何在 GAE 任务队列中执行需要 OAuth 的操作?
【发布时间】:2013-05-30 17:29:08
【问题描述】:

我有一个简单的 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_playlist() 方法完成了它的工作,但事实证明,在某些情况下它可以运行很长时间,导致DeadlineExceededError。因此,在阅读了可用选项之后,我认为 Task Queue API 是可行的方法(对吗?),我正在尝试使用它,遵循 Using Push Queues in Python 指南。

→ 简而言之,我将UpdatePage 拆分为UpdatePageHandler + 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
...

我能做什么?感谢您的帮助!

【问题讨论】:

    标签: python google-app-engine google-api task-queue google-api-python-client


    【解决方案1】:

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

    更新:(这是在原始帖子之后添加的。)由于没有 cookie,标识用户的 SACSID cookie 将不存在,因此 decorator.oauth_required 名称将每次在 cron 作业运行时强制重定向(即 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')
    

    并使用它来获取该用户的凭据as is done in the decorator

    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()
    

    【讨论】:

    • 成功了,再次感谢!这为一个新问题打开了大门:手动打开/update 有效,但现在我想安排一个每天晚上查询它的 cron 任务。我想这意味着使用users.get_current_user() 是不可能的,我必须缓存它?
    • 首先,你应该把这个放在你的问题中或者问一个新问题。如果您希望 cron 作业以特定用户身份运行,您只需将 user_id 硬编码到脚本中即可。如果您希望它为所有用户运行,您可以从 CredentialsModel 查询所有存储的凭据,然后为每个用户生成一个任务。
    • 是的,抱歉。我已经问了很多,所以我想我只是把这个问题总结在这里,这确实是个坏主意。我试过你的建议,但我的 cron 任务总是失败;我正在跟进一个适当的新问题:How to do OAuth-requiring operations in a GAE cron job?
    猜你喜欢
    • 2013-05-27
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多