在Webapp2 / Google App Engine

时间:2015-07-06 21:51:06

标签: python google-app-engine redirect oauth-2.0

我正在AppEngine / Python上编写一个小型Web应用程序来访问GitHub API。

我已经成功执行了Oauth2流程(即我可以访问已记录的用户信息)。我现在要做的是,当用户回到我指定为gitHub的redirect_uri的网页uri,授权应用程序时,我发出获取访问令牌的请求,然后将用户重定向到主页。

现在,如果我在redirect_uri的处理程序末尾使用self.redirect("/")执行重定向,Python会收到错误AttributeError: url

我做错了什么?

这是redirect_uri

的处理程序的类定义
class RedirectGithub(webapp2.RequestHandler):
    def get(self):
        self.github_code = self.request.get("code")
        self.payload = {
        "client_id": GITHUB_CLIENT_ID, 
        "client_secret": GITHUB_CLIENT_SECRET,  
        "code": self.github_code, 
        "redirect_uri": GITHUB_REDIRECT_URI, 
    }

    self.data = urllib.urlencode(self.payload)
    self.request = urllib2.Request(GITHUB_URL_ACCESSTOKEN, self.data)
    self.github_response = urllib2.urlopen(self.request)
    github_access_token = urlparse.parse_qs(self.github_response.read())
    self.redirect("/")

这是完整的堆栈跟踪

    Traceback (most recent call last):
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
        rv = self.handle_exception(request, response, e)
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
        rv = self.router.dispatch(request, response)
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
        return route.handler_adapter(request, response)
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
        return handler.dispatch()
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
        return self.handle_exception(e, self.app.debug)
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
        return method(*args, **kwargs)
      File "/base/data/home/apps/MY_APP", line 125, in get
        self.redirect("/")
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 583, in redirect
        response=self.response)
      File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1740, in redirect
        uri = str(urlparse.urljoin(request.url, uri))
      File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 229, in __getattr__
        raise AttributeError, attr
    AttributeError: url

1 个答案:

答案 0 :(得分:1)

[更新回答]

当webapp2使用'request'对象的url时出现问题:

if uri.startswith(('.', '/')):
    request = request or get_request()
    uri = str(urlparse.urljoin(request.url, uri))

在这种情况下,您使用自己的(调用GitHub)覆盖RequestHandler的'self.request'属性:

self.request = urllib2.Request(GITHUB_URL_ACCESSTOKEN, self.data)

我猜测这个新的请求对象没有'url'。

我建议你使用不同的变量名,或者不要在'self'上存储Github请求。

=======================

[旧答案]

根据给定的信息在这里走出困境,但你的处理程序类是否扩展了webapp2.RequestHandler?如果不是,则'self'中可能不存在url属性。

如果不是这种情况,请包括您的处理程序类定义和您的(最小)处理程序方法。

即:

class SomeHandler(webapp2.RequestHandler):
   ...