使用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
我是什麼錯過了導致這個錯誤?
它可能是值得在谷歌庫中啓用http跟蹤,所以你可以看到電線上發生了什麼。 OAuth庫喜歡混淆基礎錯誤。我的猜測會是你的回調網址不正確。 – pinoyyid 2014-09-24 07:42:36
+1對''沒有太多的文檔,但[這是所有廢話]'。對於沒有文件的通常投訴確實是相反的。在這裏,它是乾草堆尋找你需要的信息的金塊。 – 2014-09-26 07:40:58