当我从 main.py 进行重定向时,它可以工作,但是当我尝试从它调用的方法中进行重定向时,什么也没有发生。没有错误,程序什么都不做。
主.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
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')
最佳答案
这取决于你在 Github 上做什么样的授权,有两种方法可以做到这一点,OAuth token 授权和Web 应用程序流程。
OAuth token 授权
如果您正在执行 OAuth 授权,则不必创建请求处理程序来获取 Github 身份验证 token ,请求处理程序用于在您的服务器上提供特定的 url,对于此类任务,您应该使用 urlfetch()
.
所以整个流程应该像下面的代码:
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...
重定向授权(Web 应用程序流程)
如果你已经申请了client id,想要作为一个独立的web应用被用户授权,这种授权的步骤比前一种要复杂一些:
- 重定向用户以请求 GitHub 访问权限
- GitHub 重定向回您的站点
如果你不知道这个流程,看看Github OAuth - Web Application Flow
让我们看看我们如何在 Google App Engine 中做
重定向用户请求访问 Github
这是您的示例中涉及的部分,只需将用户重定向到具有指定参数的授权 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 将用户重定向回您的站点
Github 会根据之前的参数 redirect_url
将用户重定向回您的站点,因此您必须准备另一个请求处理程序来接收来自 Github 的重定向。
(您可以使用相同的请求处理程序来执行此操作,但它会弄乱您的代码)
从第 1 步返回的重定向将包含一个参数,code
,您将需要它来交换访问 token 。
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 :)
关于python - GAE/Python - 从主类而不是从被调用的方法重定向工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22698404/