[英]GAE/Python - redirect working from main class but not from called method
When I make a redirect from main.py, it works, but when I try to redirect from within a method that it calls, nothing happens. 当我从main.py进行重定向时,它可以工作,但是当我尝试从其调用的方法中进行重定向时,什么也没有发生。 There is no error, the program simply does nothing. 没有错误,程序只是不执行任何操作。
main.py main.py
from githubauth import GetAuthTokenHandler
class AuthUser(webapp2.RequestHandler):
"""
If no environment variable exists with the access token in it,
auth the user as an admin. If it does exist, auth them as a regular
user.
"""
def get(self):
if not ACCESS_TOKEN:
# No access token exists, auth user as admin
get_auth_token = GetAuthTokenHandler()
get_auth_token.get()
githubauth.py githubauth.py
import webapp2
class GetAuthTokenHandler(webapp2.RequestHandler):
"""Redirect users to github to get an access request token."""
def get(self):
self.redirect('http://api.github.com/authorize')
It depends on what kind of authorization you're doing with Github, there are two ways to do that, OAuth token authorization and Web Application Flow . 这取决于您对Github进行的授权类型,可以通过两种方式进行: OAuth令牌授权和Web应用程序流程 。
If you're doing OAuth authorization, you don't have to create a request handler to fetch Github auth token, request handler is for serving specific url on your server, for this kind of task, you should use urlfetch()
. 如果您正在执行OAuth授权,则无需创建请求处理程序来获取Github身份验证令牌,请求处理程序用于在服务器上提供特定的url,对于此类任务,您应该使用urlfetch()
。
So the whole flow should be like the following code: 因此,整个流程应类似于以下代码:
import webapp2
from google.appengine.api import urlfetch
def getAuthToken():
github_auth_url = "http://api.github.com/authorizations"
result = urlfetch.fetch(github_auth_url)
return result
class AuthUser(webapp2.RequestHandler):
def get(self):
if not ACCESS_TOKEN:
# No access token exists, auth user as admin
get_auth_token = getAuthToken()
# do something with your token...
This is the case if you have applied a client id, and want to be authorized by users as a standalone web application, the steps of this kind authorization is more complicated than former one: 如果您已经应用了客户端ID,并且希望被用户授权为独立的Web应用程序,则是这种情况,这种授权的步骤比以前的授权要复杂得多:
If you don't know about this flow, take a look at Github OAuth - Web Application Flow 如果您不了解此流程,请查看Github OAuth-Web应用程序流程
Let's see how could we do within Google App Engine 让我们看看我们如何在Google App Engine中进行操作
This is the part which involved in your sample, simply redirect user to the authorize url with specified parameters 这是样本中涉及的部分,只需将用户重定向到具有指定参数的授权url
from urllib import urlencode
class AuthUser(webapp2.RequestHandler):
def get(self):
# ... do something ...
# Github configuration
github_client_id = "Your github client id..."
github_redirect_url = "Your url for github redirecting users back to your GAE"
github_scope = "Gtihub scopes...."
github_authorize_url = "http://github.com/login/oauth/authorize"
github_authorize_parameters = {
'client_id': github_client_id,
'redirect_url': github_redirect_url,
'scope': github_scop
}
if not ACCESS_TOKEN:
# if no access_token found on your site, redirect users to Github for authorization
url_to_redirect = "%s?%s" % (github_authorize_url, urlencode(github_authorize_parameters))
self.redirect(url_to_redirect)
Github will redirect users back to your site based on the previous parameter redirect_url
, so you will have to prepare another request handler for receiving redirection from Github. Github将根据先前的参数redirect_url
将用户重定向回您的站点,因此您将必须准备另一个请求处理程序以接收来自Github的重定向。
(You can do this is the same request handler, but it will mess your code) (您可以执行相同的请求处理程序,但这会弄乱您的代码)
The redirection back from the step 1 will contains one parameter, code
, you will need it to exchange for an access token. 从步骤1返回的重定向将包含一个参数code
,您将需要它来交换访问令牌。
from urllib import urlencode
class GithubRequestHandler(webapp2.RequestHandler):
def get(self):
# this handler need to be bind to redirect_url
# get authentication code
github_code = self.request.get('code')
# prepare data to exchange access token
github_token_url = "https://github.com/login/oauth/access_token"
github_token_parameters = {
'client_id': 'Your Github client id',
'client_secret': 'Your Github client secret',
'code': github_code}
# exchange access token
data = urlfetch.fetch(github_token_url, payload=urlencode(github_token_parameter), method='POST')
# data will perform in the following form:
# access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&scope=user%2Cgist&token_type=bearer
# extract access_token from the string
# save the access_token to user's model
the code is kinda of simulation of your application flow, it needs some tuning to be able to run on production :) 该代码有点像您的应用程序流程的模拟,它需要进行一些调整才能在生产环境中运行:)
RequestHandler needs to be instantiated with a request and a response for things to work properly. 需要使用请求和响应实例化RequestHandler,以使事情正常运行。 That said, instantiating one and calling methods on it from inside the handler-method of another is pretty weird. 也就是说,从另一个的处理程序方法内部实例化一个实例并对其调用方法是很奇怪的。
You try to create a webapp2 request handler, but it cannot be done this way. 您尝试创建一个webapp2请求处理程序,但是无法通过这种方式完成。 get_auth_token is not a WSGI webapp2 handler instance. get_auth_token不是WSGI webapp2处理程序实例。 If you do not change githubauth.py you have to change your main.py. 如果不更改githubauth.py,则必须更改main.py。
class AuthUser(webapp2.RequestHandler):
def get(self):
if not ACCESS_TOKEN:
self.redirect(to your GetAuthTokenHandler)
This will result in two redirects if you do not have an access token. 如果您没有访问令牌,这将导致两次重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.