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();
// Note, unlike in the Android app below, there's no 'oauth2:' prefix here
String SCOPE = "https://www.googleapis.com/auth/userinfo.email";
// magic happens here
User user = oauth.getCurrentUser(SCOPE);
下面是完整的Python2.7处理程序,它允许您验证用户:
from google.appengine.api import oauth
import logging
import traceback
import webapp2
class MainHandler(webapp2.RequestHandler):
def post(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hi there!\n')
# Note, unlike in the Android app below, there's no 'oauth2:' prefix here
scope = 'https://www.googleapis.com/auth/userinfo.email'
try:
self.response.write('\noauth.get_current_user(%s)' % repr(scope))
# validates audience of the OAuth2 access token
allowed_clients = ['407408718192.apps.googleusercontent.com'] # list your client ids here
token_audience = oauth.get_client_id(scope)
if token_audience not in allowed_clients:
raise oauth.OAuthRequestError('audience of token \'%s\' is not in allowed list (%s)' % (token_audience, allowed_clients))
# gets user object for the user represented by the oauth token
user = oauth.get_current_user(scope)
self.response.write(' = %s\n' % user)
self.response.write('- auth_domain = %s\n' % user.auth_domain())
self.response.write('- email = %s\n' % user.email())
self.response.write('- nickname = %s\n' % user.nickname())
self.response.write('- user_id = %s\n' % user.user_id())
except oauth.OAuthRequestError, e:
self.response.set_status(401)
self.response.write(' -> %s %s\n' % (e.__class__.__name__, e.message))
logging.warn(traceback.format_exc())
app = webapp2.WSGIApplication([
('/.*', MainHandler)
], debug=True)
在Python和Java应用程序引擎运行时内置OAuth2支持。
在Python中,您只需要:
在Java中,您可以使用:
下面是完整的Python2.7处理程序,它允许您验证用户:
app.yaml很简单
注意,客户机应该在
Authorization: Bearer
HTTP请求头中发送OAuth2令牌,例如如果您碰巧正在构建一个Android应用程序,则可以使用
AccountManager
界面轻松生成这些令牌:如果您想看到所有东西都放在一起,请随时查看这些项目的完整来源:
你查过OAuth for Python文章信息了吗?它说是为了“这个参考描述了如何将OAuth与Python应用程序一起用作服务提供者。”
我无法对上述答案发表评论,因此我在这里为所有与此片段纠结的人添加了答案:
如果您使用服务帐户(从今天起,我认为还有Google用户令牌)作为令牌长度会导致AE库中出现问题,那么AppEngine上的这个问题已经中断了一个月。谷歌告诉我,他们不太可能很快修复它。
这是目前唯一对我有效的方法:
相关问题 更多 >
编程相关推荐