"AttributeError: url" when calling self.redirect("/") on Webapp2/Google App Engine
"AttributeError: url" when calling self.redirect("/") on Webapp2/Google App Engine
我正在 AppEngine/Python 上编写一个小型网络应用程序以访问 GitHub API。
我已成功执行 Oauth2 流程(即,我可以访问记录的用户信息)。我现在想做的是,当用户返回到我指定为 GitHub 的 redirect_uri 的网页 uri 时,已授权该应用程序,我请求获取访问令牌然后将用户重定向到主页。
现在,如果我在 redirect_uri 的处理程序末尾使用 self.redirect("/")
执行重定向,Python 会抛出错误 AttributeError: url
。
我做错了什么?
这是 redirect_uri
的处理程序的 class 定义
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
[更新答案]
当 'request' 对象的 url 被 webapp2 使用时出现问题:
if uri.startswith(('.', '/')):
request = request or get_request()
uri = str(urlparse.urljoin(request.url, uri))
在这种情况下,您将用自己的属性覆盖 RequestHandler 的 'self.request'(调用 GitHub):
self.request = urllib2.Request(GITHUB_URL_ACCESSTOKEN, self.data)
我推测这个新请求对象上没有'url'。
我建议您使用不同的变量名,或者不要将 Github 请求存储在 'self' 上。
=======================
[旧答案]
根据给定的信息,这里有点危险,但是你的处理程序 class 扩展 webapp2.RequestHandler 了吗?如果不是,url 属性可能不存在于 'self'.
中
如果不是这种情况,请包括您的处理程序 class 定义和您的(最小)处理程序方法。
即:
class SomeHandler(webapp2.RequestHandler):
...
我正在 AppEngine/Python 上编写一个小型网络应用程序以访问 GitHub API。
我已成功执行 Oauth2 流程(即,我可以访问记录的用户信息)。我现在想做的是,当用户返回到我指定为 GitHub 的 redirect_uri 的网页 uri 时,已授权该应用程序,我请求获取访问令牌然后将用户重定向到主页。
现在,如果我在 redirect_uri 的处理程序末尾使用 self.redirect("/")
执行重定向,Python 会抛出错误 AttributeError: url
。
我做错了什么?
这是 redirect_uri
的处理程序的 class 定义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
[更新答案]
当 'request' 对象的 url 被 webapp2 使用时出现问题:
if uri.startswith(('.', '/')):
request = request or get_request()
uri = str(urlparse.urljoin(request.url, uri))
在这种情况下,您将用自己的属性覆盖 RequestHandler 的 'self.request'(调用 GitHub):
self.request = urllib2.Request(GITHUB_URL_ACCESSTOKEN, self.data)
我推测这个新请求对象上没有'url'。
我建议您使用不同的变量名,或者不要将 Github 请求存储在 'self' 上。
=======================
[旧答案]
根据给定的信息,这里有点危险,但是你的处理程序 class 扩展 webapp2.RequestHandler 了吗?如果不是,url 属性可能不存在于 'self'.
中如果不是这种情况,请包括您的处理程序 class 定义和您的(最小)处理程序方法。
即:
class SomeHandler(webapp2.RequestHandler):
...