Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 谷歌应用引擎oauth2提供商_Python_Google App Engine_Oauth 2.0 - Fatal编程技术网

Python 谷歌应用引擎oauth2提供商

Python 谷歌应用引擎oauth2提供商,python,google-app-engine,oauth-2.0,Python,Google App Engine,Oauth 2.0,我想用OAuth2.0提供程序设置一个RESTAPI进行身份验证。我使用python。 是否有任何库用于设置在app engine上运行的用python编码的oauth 2.0提供程序? 谢谢。您签出文章信息了吗?它说它是为了“此参考描述了如何将OAuth与Python应用程序一起用作服务提供者。”OAuth2在Python和Java应用程序引擎运行时的内置支持 在Python中,您只需要: from google.appengine.api import oauth # Note, unli

我想用OAuth2.0提供程序设置一个RESTAPI进行身份验证。我使用python。 是否有任何库用于设置在app engine上运行的用python编码的oauth 2.0提供程序?
谢谢。

您签出文章信息了吗?它说它是为了“此参考描述了如何将OAuth与Python应用程序一起用作服务提供者。”

OAuth2在Python和Java应用程序引擎运行时的内置支持

在Python中,您只需要:

from google.appengine.api import oauth

# Note, unlike in the Android app below, there's no 'oauth2:' prefix here
SCOPE = 'https://www.googleapis.com/auth/userinfo.email'

# magic happens here
user = oauth.get_current_user(SCOPE)
在Java中,您将使用:

OAuthService oauth=OAuthServiceFactory.getOAuthService();
//注意,与下面的Android应用程序不同,这里没有“oauth2:”前缀
字符串范围=”https://www.googleapis.com/auth/userinfo.email";
//魔法在这里发生
User User=oauth.getCurrentUser(范围);
下面是完整的Python 2.7处理程序,它将允许您验证用户:

从google.appengine.api导入oauth
导入日志记录
导入回溯
导入webapp2
类MainHandler(webapp2.RequestHandler):
def post(自我):
self.response.headers['Content-Type']='text/plain'
self.response.write('Hi there!\n')
#注意,与下面的Android应用程序不同,这里没有“oauth2:”前缀
范围=https://www.googleapis.com/auth/userinfo.email'
尝试:
self.response.write('\noauth.get_当前用户(%s)“%repr(作用域))
#验证OAuth2访问令牌的访问群体
允许的客户端=['407408718192.apps.googleusercontent.com']#在此处列出您的客户端ID
令牌\u访问群体=oauth.get\u客户端\u id(范围)
如果令牌\u访问群体不在允许的\u客户端中:
raise oauth.OAuthRequestError('token\'%s\'的访问群体不在允许的列表(%s)'(token\u访问群体,允许的\u客户端))
#获取由oauth令牌表示的用户的用户对象
user=oauth.get\u当前用户(范围)
self.response.write('=%s\n“%user”)
self.response.write('-auth\u domain=%s\n'%user.auth\u domain())
self.response.write('-email=%s\n'%user.email())
self.response.write('-nickname=%s\n'%user.nickname())
self.response.write('-user\u id=%s\n'%user.user\u id())
除了oauth.OAuthRequestError,e:
self.response.set_状态(401)
self.response.write('->%s%s\n'%(e.\u class\u.\u name\u,e.message))
logging.warn(traceback.format_exc())
app=webapp2.WSGIApplication([
('/.*',主处理器)
],debug=True)
app.yaml是微不足道的

application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app
注意,客户端应该在
授权:承载
HTTP请求头中发送OAuth2令牌,例如

Authorization: Bearer ya29XAHES6ZT4w72FecXjZu4ZWskTSX3x3OqYxUSTIrA2IfxDDPpI
如果您碰巧正在构建Android应用程序,您可以使用
AccountManager
界面轻松生成这些令牌:

AccountManager=AccountManager.get(这个);
Account[]accounts=accountManager.getAccountsByType(“com.google”);
//TODO:允许用户指定要使用哪个帐户进行身份验证
用于(账户:账户){
Log.i(标记“-account.name=“+account.name”);
}
//注意这里的“oauth2:”前缀
字符串authTokenType=“oauth2:https://www.googleapis.com/auth/userinfo.email";
//注意:AccountManager将缓存这些令牌,即使它们已过期。
//TODO:在身份验证失败后或通过以下方式使过期令牌无效:
//accountManager.invalidateAuthToken(帐户[0]。类型,标记);
accountManager.getAuthToken(帐户[0],authTokenType,null,this,
新建AccountManagerCallback(){
@凌驾
公共作废运行(AccountManagerFuture){
试一试{
String token=future.getResult().getString(AccountManager.KEY\u AUTHTOKEN);
Log.i(标记“Got KEY_AUTHTOKEN:+token”);
//不要忘记HTTP头文件“Authorization:Bearer”

callAppEngineRestApi(token);//我无法对上述答案发表评论,因此我在这里添加了它,以供任何与此代码片段有冲突的人使用:

# magic happens here
user = oauth.get_current_user(SCOPE)
如果你使用的是服务帐户(从今天起,我认为也是谷歌用户代币),这一点在AppEngine上已经被打破了一个月,因为代币长度会导致AE库出现问题。谷歌告诉我,他们不太可能很快解决这个问题

这是目前唯一对我有效的方法:

    token = self.request.headers['Authorization'].split(' ')[1]
    url = 'https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=' + token
    oauth_response = urlfetch.fetch(url)
    if oauth_response.status_code != 200:
        raise Exception('Unable to authorise: {}/{}'.format(oauth_response.status_code, oauth_response.content))
    token_response = json.loads(oauth_response.content)
    email = token_response['email']

它使用google进行身份验证,而不是我的服务,这使得我的身份验证完全依赖于google!然后看看Python中完整的OAuth实现。我认为,尽管名称不同,但它只能用于创建OAuth 1.0 Providers。你确定吗?Afaik Gae只支持OAuth 1.0aI。你的代码已经测试过了,但它不是作品-例外:-我使用的是Python2.5。对Google和StackOverflow进行了大量的研究。这是我找到的唯一一个具有良好工作代码的答案。(有关完整示例,请参阅提供的URL)。也为Java工作,因此问题标题可能会误导Java用户。令人惊讶。我花了数周时间尝试在python appengine上使用OAuth 2.0,这似乎是唯一一篇讨论OAuth.get\u current\u用户(范围)的文章
。它甚至不在文档中,但对我来说,这确实很有魅力!这是做什么的?背后的逻辑是什么?它使用google rest api“解码”承载令牌,从响应中提取电子邮件地址。