python - 如何正确定义GAE的oauth2callback?

标签 python google-app-engine oauth-2.0 google-api-python-client

Using GAE / Decorators指南告诉我“您需要向您的应用程序添加特定的 URL 处理程序来处理从授权服务器返回到您的应用程序的重定向”:

  def main():
    application = webapp.WSGIApplication(
       [
         ('/', MainHandler),
         ('/about', AboutHandler),
         (decorator.callback_path, decorator.callback_handler()),
       ],
       debug=True)
    run_wsgi_app(application)

目前我无法正确设置此设置。结果,我得到并看到了 HTTP 302 回调响应(虽然它应该被处理程序捕获),而不是我期望的响应。我有两个问题要解决:

  1. GAE 1.8.0 中的 oauth2client/appengine.py 没有 callback_path 属性,也没有 callback_handler() 方法,我们该怎么办应该做的?直接绑定(bind)('/oauth2callback', OAuth2Handler)而不是(decorator.callback_path,decorator.callback_handler())
  2. 这对 myapp.yaml 意味着什么?声明一个新 block 是否正确:
    - url: /oauth2callback
      script: oauth2client/appengine.py

Thanks for your help! Here is my current code:


myapp.py

class UpdatePage(webapp2.RequestHandler):

    def get(self):
        playlist_id = self.youtube_create_playlist()
        ...

    @decorator.oauth_required
    def youtube_create_playlist(self):
        http = decorator.http()
        request = youtube.playlists().insert(...)
        response = request.execute(http=http)
        return response["id"]

    ...

    update = webapp2.WSGIApplication([
                                      ('/update', UpdatePage),
                                      ('/oauth2callback', OAuth2Handler)
                                      ],
                                     debug=True)

app.yaml

application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: false

handlers:
- url: /
  static_files: index.html
  upload: index.html

- url: /oauth2callback
  script: oauth2client/appengine.py

- url: /update
  script: myapp.update

最佳答案

该库未在 App Engine 中提供。

您应该使用的版本是 project download page 上托管的 google-api-python-client-1.1 .

我相信您所指的版本是 google-api-python-client included in the App Engine SDK 的(有点旧)版本。它仅用于为 appcfg.py 执行简单的 OAuth 2.0,并且是执行此简单任务的稳定版本。尽管它位于 SDK 中,但不在运行时中,并且由于这些原因,它未被认可为 google-api-python-client 的当前版本。

我还想指出,您明确链接的文章指向 installation instructions .

更新:如前所述,您的 WSGI 处理程序应包含来自装饰器的回调

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

并且您的app.yaml应该允许您的主处理程序应该显式匹配decorator.callback_path中的路由

- url: /oauth2callback
  script: myapp.update

或者应该将所有剩余请求路由到您的 WSGI 处理程序

- url: /.*
  script: myapp.update

(第二种方法可能需要向 WSGI 处理程序添加 404 包罗万象。)

关于python - 如何正确定义GAE的oauth2callback?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16799363/

相关文章:

python - 如何在不更新字典(Python)的情况下检查 defaultdict 中的键?

python - 如何在 ubuntu 17 上安装 ipython notebook?

java - Google DataStore - 如何按 ID/名称获取数据?

Azure Active Directory - 允许的 token 受众

python - mysqlclient偶尔崩溃,建立TLS连接

python - pyad:安装正常,但说找不到 adbase

java - Google App Engine 错误 DeadlineExceededException

google-app-engine - Appengine在中国能用吗?

spring-security - 为什么资源服务器必须知道 Spring OAuth2 中的 client_id?

oauth - 如何在 postman 收藏中保留OAuth2 token (或使用刷新 token )?