【问题标题】:"AttributeError: url" when calling self.redirect("/") on Webapp2/Google App Engine在 Webapp2/Google App Engine 上调用 self.redirect("/") 时出现“AttributeError: url”
【发布时间】:2015-07-06 21:51:05
【问题描述】:

我正在 AppEngine/Python 上编写一个用于访问 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

【问题讨论】:

  • 我建议您包括堆栈跟踪,而不仅仅是错误。

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


【解决方案1】:

[更新答案]

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

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”。

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

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

[旧答案]

根据给定的信息,这里有点危险,但是您的处理程序类是否扩展了 webapp2.RequestHandler?如果不是,'self' 中可能不存在 url 属性。

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

即:

class SomeHandler(webapp2.RequestHandler):
   ...

【讨论】:

  • 是的,该类扩展了 webapp2.RequestHandler。我已按照您的建议添加了类定义,谢谢。
  • 太棒了!非常感谢!
猜你喜欢
  • 2017-05-01
  • 2014-01-20
  • 2011-10-10
  • 2015-01-07
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 2012-07-13
相关资源
最近更新 更多