如何正确定义GAE的oauth2callback? [英] How to properly define GAE's oauth2callback?

查看:110
本文介绍了如何正确定义GAE的oauth2callback?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GAE /装饰者指南告诉我你需要添加一个特定的URL处理程序到你的应用程序来处理从授权服务器重定向到你的应用程序

<$ p $ def main():
application = webapp.WSGIApplication(
[
('/',MainHandler),
('/ about',关于处理程序),
(decorator.callback_path,decorator.callback_handler()),
],
debug = True)
run_wsgi_app(应用程序)

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


  1. oauth2client / appengine.py 运输在GAE 1.8.0中没有 callback_path 属性,也没有 callback_handler()方法,我们该怎么做?直接绑定('/ oauth2callback',OAuth2Handler)而不是(decorator.callback_path,decorator.callback_handler())

  2. 这对 myapp.yaml 来说意味着什么?是否正确地声明一个新的块:

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


感谢您的帮助!以下是我目前的代码:




myapp.py

 类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)
返回响应[id]

...

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

app.yaml

 应用程序:myapp 
版本:1
运行时:python27
api_v ersion:1
threadsafe:false

处理程序:
- url:/
static_files:index.html
上传:index.html

- url:/ oauth2callback
脚本:oauth2client / appengine.py
$ b - url:/ update
脚本:myapp.update
$ b

解决方案

此库不在App Engine中发布。



您应该使用的版本是 google-api-python-client-1.1 。 com / p / google-api-python-client / downloads / detail?name = google-api-python-client-gae-1.1.ziprel =nofollow>项目下载页面



我相信你指的版本是 google-api-python-client 。这仅包含在 appcfg.py 中执行简单的OAuth 2.0,并且是执行此简单任务的稳定版本。虽然它在SDK中,但它在运行时是 NOT ,并且没有被认可为当前版本的 google-api-python-client 原因。



我还想指出,您明确链接的文章指向安装说明


$ b

更新:在那里,你的WSGI处理程序应该包含来自装饰器的回调。

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

和你的 app.yaml 应该允许你的主处理器或者明确地匹配 decorator.callback_path

   -  url:/ oauth2callback 
脚本: myapp.update

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

   -  url:/.* 
script:myapp.update

(第二种方法可能有必要向WSGI处理程序添加一个404 catch-all。)


The Using GAE / Decorators guide tells me that "you need to add a specific URL handler to your application to handle the redirection from the authorization server back to your application":

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

Currently I am unable to properly set this up. As a result, I get and see the HTTP 302 callback response (while it should be caught by the handler) instead of the response I'm expecting. I have two questions to address that:

  1. The oauth2client/appengine.py shipping in GAE 1.8.0 has no callback_path attribute and no callback_handler() method, what are we supposed to do? Directly bind ('/oauth2callback', OAuth2Handler) instead of (decorator.callback_path, decorator.callback_handler())?
  2. What does that imply for myapp.yaml? Is it right to declare a new block like:

    - 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

解决方案

This library does not ship in App Engine.

The version you should be using is google-api-python-client-1.1 hosted on the project download page.

I believe the version you're referring to is the (somewhat old) version of google-api-python-client included in the App Engine SDK. This is only included to perform simple OAuth 2.0 for appcfg.py and is a stable version for performing this simple task. Though it is in the SDK, it is NOT in the runtime and not endorsed as a current version of google-api-python-client for these reasons.

I'd also like to note that the article you linked explicitly points to installation instructions.

UPDATE: As noted there, your WSGI handler should contain the callback from the decorator

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

and your app.yaml should allow your main handler should either explicitly match the the route in decorator.callback_path

- url: /oauth2callback
  script: myapp.update

or should just route all remaining requests to your WSGI handler

- url: /.*
  script: myapp.update

(This second approach would likely warrant adding a 404 catch-all to the WSGI handler.)

这篇关于如何正确定义GAE的oauth2callback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆