2014-09-23 46 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

相關問題