【问题标题】:User info using OAuth with Google App Engine将 OAuth 与 Google App Engine 结合使用的用户信息
【发布时间】:2014-09-25 02:48:58
【问题描述】:

在使用 OAuth 2.0 和 Python 时,我希望使用用户 ID 或电子邮件来存储/检索 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

我错过了什么导致此错误?

【问题讨论】:

  • 在 google 库中启用 http 跟踪可能是值得的,这样您就可以看到网络上发生了什么。 OAuth 库喜欢混淆底层错误。我的猜测是您的回调 URL 不正确。
  • +1 表示there is no much documentation but [it is all crap]。这确实与通常抱怨没有文件的情况相反。在这里找到您需要的信息块是大海捞针。

标签: python google-app-engine oauth google-oauth


【解决方案1】:

我刚刚完成了类似的工作。让用户使用 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 调用。

【讨论】:

猜你喜欢
  • 2012-02-26
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 2020-05-26
  • 2014-08-26
  • 2014-03-29
  • 1970-01-01
相关资源
最近更新 更多