2014-09-23 32 views
6

使用OAuth 2.0和Python时我想要用户ID或电子邮件来存储/检索OAuth访问令牌,因为即使在用户离开后我仍想修改日历。在Google App Engine中使用OAuth的用户信息

有太多的文档,其中一半已被弃用(OAuth 1.0),我无法弄清楚这一点。

我有以下代码:

import webapp2 
import os 

from apiclient.discovery import build 
from oauth2client.appengine import OAuth2DecoratorFromClientSecrets 
from google.appengine.api import oauth 

user_scope = 'https://www.googleapis.com/auth/userinfo.profile' 

decorator = OAuth2DecoratorFromClientSecrets(
    os.path.join(os.path.dirname(__file__), 'client_secrets.json'), 
    scope=('https://www.googleapis.com/auth/calendar', user_scope) 
) 

service = build('calendar', 'v3') 

class MainHandler(webapp2.RequestHandler): 
    @decorator.oauth_required 
    def get(self): 
     self.response.write('Hello world!') 

     user = oauth.get_current_user(user_scope) 
     if user: 
      self.response.write('%s\n' % user) 
      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()) 
     else: 
      self.response.write('No user found...') 


app = webapp2.WSGIApplication([ 
    ('/', MainHandler), 
    (decorator.callback_path, decorator.callback_handler()) 
], debug=True) 

这在当地工作在测试环境中,但是当我部署和在线运行它,我得到以下错误:

Traceback (most recent call last): 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch 
    return self.handle_exception(e, self.app.debug) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "/base/data/home/apps/s~myapp/oauth2client/appengine.py", line 714, in check_oauth 
    resp = method(request_handler, *args, **kwargs) 
    File "/base/data/home/apps/s~myapp/main.py", line 29, in get 
    user = oauth.get_current_user('https://www.googleapis.com/auth/userinfo.profile') 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/oauth/oauth_api.py", line 100, in get_current_user 
    _maybe_call_get_oauth_user(_scope) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/oauth/oauth_api.py", line 231, in _maybe_call_get_oauth_user 
    _maybe_raise_exception() 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/oauth/oauth_api.py", line 246, in _maybe_raise_exception 
    raise NotAllowedError(error_detail) 
NotAllowedError 

我是什么错过了导致这个错误?

+0

它可能是值得在谷歌库中启用http跟踪,所以你可以看到电线上发生了什么。 OAuth库喜欢混淆基础错误。我的猜测会是你的回调网址不正确。 – pinoyyid 2014-09-24 07:42:36

+2

+1对''没有太多的文档,但[这是所有废话]'。对于没有文件的通常投诉确实是相反的。在这里,它是干草堆寻找你需要的信息的金块。 – 2014-09-26 07:40:58

回答

4

我刚刚得到了类似的结果。一旦您将用户转到OAuth,您需要存储当前的用户ID。你可以,如果你使用任务队列

from google.appengine.api import users ClientID = users.get_current_user().user_id()

然后什么都代码,你以后需要令牌运行的OAuth通过数据存储或作为参数做到这一点。

from oauth2client.appengine import CredentialsModel 
from oauth2client.appengine import StorageByKeyName 

credentials = StorageByKeyName(CredentialsModel, ClientID, 'credentials').get() 
cal = build('calendar', 'v3',http = credentials.authorize(httplib2.Http())) 

然后用cal来做你的API调用。

+0

谢谢我现在使用这个,不幸的是有一个错误,如果你用5个或更多的用户登录,你会得到400错误。显然这个错误,但不能修复:https://code.google.com/p/googleappengine/issues/detail?id=9045和https://twitter.com/googlecloud/status/467411184430239745 – 2014-09-26 15:04:55

+0

我不知道谢谢。我将不得不关注这一点。 – Ryan 2014-09-26 17:57:19