python - 如何在 GAE cron 作业中执行需要 OAuth 的操作?

标签 python google-app-engine cron google-api google-api-python-client

这篇文章是 How to do OAuth-requiring operations in a GAE Task Queue? 的后续文章。正如 bossylobster 在这篇文章中所建议的那样,我确实尝试在我的脚本中对 user_id 进行硬编码,但 cron 任务总是失败:

  • 我可以在日志中看到它被触发了...

    2013-05-31 14:20:00.879 /update 302 5ms 0kb AppEngine-Google; (+http://code.google.com/appengine)
    0.1.0.1 - - [31/May/2013:11:20:00 -0700] "GET /update HTTP/1.1" 302 385 - "AppEngine-Google; (+http://code.google.com/appengine)" "myapp.appspot.com" ms=6 cpu_ms=0 cpm_usd=0.000043 queue_name=__cron task_name=... app_engine_release=1.8.0 instance=...
    
  • ...然而,它总是失败(在 AppEngine 的 Cron Jobs 部分显示为红色“失败”),并且它应该运行的任务队列永远不会弹出在任务队列部分。

我不明白的是,如果我手动打开 /update,我会在下面看到登录提示。 cron 应该如何绕过它?我如何调试正在发生的事情?

GAE login prompt

感谢您的帮助。

编辑/跟进@dlebech 的回答:在 中向相关 url 添加 login: admin 行后问题仍然存在应用程序.yaml。在本地(通过以管理员身份登录并在浏览器中访问处理程序的 URL,如 Securing URLs for Cron 所述)一切正常,但一旦我部署,我的应用程序永远不会通过 OAuth2 装饰器:

  • 使用一些调试 print 语句...

    import webapp2
    ...
    
    YOUTUBE_RW_SCOPE = "https://www.googleapis.com/auth/youtube"
    YOUTUBE_API_SERVICE_NAME = "youtube"
    YOUTUBE_API_VERSION = "v3"
    print "Building youtube object"
    print "Built youtube object"
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
    print "Building decorator object"
    decorator = OAuth2DecoratorFromClientSecrets(CLIENT_SECRETS, YOUTUBE_RW_SCOPE)
    print "Built decorator object"
    ...
    
    class FetchHandlerPage(webapp2.RequestHandler):
        print "Entering FetchHandlerPage"
    
        @decorator.oauth_required
        def get(self):
            print "Entering FetchHandlerPage:get"
            gae_user_id = USER_ID
            query_string = urlencode({'user_id': gae_user_id})
            taskqueue.add(url='/fetchworker?' + query_string, method='GET')
    
  • ...这是部署后我在 GAE 日志中看到的内容(日志在最后一行停止,接下来什么也没有发生):

    2013-05-31 16:12:02.765 /fetch 302 1746ms 0kb AppEngine-Google; (+http://code.google.com/appengine)
    0.1.0.1 - - [31/May/2013:13:12:02 -0700] "GET /fetch HTTP/1.1" 302 385 - "AppEngine-Google; (+http://code.google.com/appengine)" "myapp.appspot.com" ms=1747 cpu_ms=844 cpm_usd=0.000070 queue_name=__cron task_name=... loading_request=1 app_engine_release=1.8.0 instance=...
    I 2013-05-31 16:12:02.444 URL being requested: https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest?userIp=0.1.0.1
    E 2013-05-31 16:12:02.764 Building youtube object
    E 2013-05-31 16:12:02.764 Built youtube object
    E 2013-05-31 16:12:02.764 Building decorator object
    E 2013-05-31 16:12:02.764 Built decorator object
    E 2013-05-31 16:12:02.764 End static variables
    E 2013-05-31 16:12:02.764 Entering FetchHandlerPage
    I 2013-05-31 16:12:02.765 This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This requ
    

最佳答案

How is cron supposed to bypass that?

确保 cron url 配置为 login: adminnot login: required。 Cron 作业会自动“通过登录提示”。参见 the documentation获取更多信息。

How can I debug what's going on?

由于 cron 作业是由对特定 url 的 GET 请求启动的,因此您应该能够通过对该 url 发出 GET 请求来调试该作业,并且查看开发服务器发生的错误或查看 GET 请求的日志中的内容。

关于python - 如何在 GAE cron 作业中执行需要 OAuth 的操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16863661/

相关文章:

google-app-engine - Azure AD 与 GAE 集成

google-app-engine - AppEngine 的 RESTFul 服务框架

linux - crontab 不能与 wget 一起使用

python - 从 C# 执行 python 文件时没有输出

python - 将 heroku django 应用程序与 ec2 postgres 数据库连接

python - PyQt 多个 tablewidgets 和 tabwidgets

python - 在 Python 中运行 shell 脚本一段给定的时间

python - 如何删除/root/.local/lib/python2.7?

java - Google App Engine 投影查询返回 0 个结果

Azure Webjobs 忽略 CRON 表达式