GAE中的OAuth2身份验证访问Calendar API V3(域托管)

时间:2012-12-11 02:41:55

标签: python google-app-engine oauth-2.0 google-calendar-api google-api-python-client

我正在使用Python开发Google App Engine应用程序。我正在使用:

  • Google Calendar API v3(用于访问我自己域中的日历。因此,这是我的域中安装的Google Apps)
  • 适用于Python的Google API客户端库。
  • OAuth2用于验证我的域用户(name@mydomain.com)

我认为我必须使用服务帐户,因为:

“如果您的App Engine应用程序需要调用API来访问应用程序项目所拥有的数据,则可以使用服务帐户简化OAuth 2.0”

取自https://developers.google.com/api-client-library/python/platforms/google_app_engine#ServiceAccounts

但我不确定我是否误解了某些事情。我的方案(GAE应用尝试在我自己的域中访问Google Apps)是服务帐户的候选人吗?

我尝试了几种处理OAuth2的方法:

  • 使用服务帐户,如上所述
  • 使用Python API客户端库提供的Python装饰器(OAuth2Decorator和OAuth2DecoratorFromClientSecrets)

在这两种情况下,我都会遇到同样的错误:

我完全迷失了。有线索吗?

非常感谢提前

2 个答案:

答案 0 :(得分:10)

您不需要服务帐户,但使用服务帐户可能很有用。 App Engine上的服务帐户存在一些棘手问题,详细信息在reported issue中。尝试使用Google APIs explorer,然后看看是否有助于阐明如何使用API​​。

只要您使用可以访问这些日历的帐户授权应用程序,您就可以访问它们,无论这是否在Google App Engine上。

使用OAuth2Decorator是您最好的选择。如果您举一个具体的例子,我很乐意提供一些代码片段来完成任务。

查看最近提出的类似问题:How can I log in to an arbitrary user in appengine for use with the Drive SDK?这似乎是您的使用案例,除非您想使用Calendar API而不是Drive API。

<强>更新

在阅读了other post之后(如果我是你,我会考虑关闭),我已经拼凑了一个样本,可以帮助您了解如何使用装饰器。

首先,要使用您的凭据,以便您的应用可以让用户授权:

from apiclient.discovery import build
import json
from oauth2client.appengine import OAuth2Decorator
import webapp2

decorator = OAuth2Decorator(
  client_id='your_client_id',
  client_secret='your_client_secret',
  scope='https://www.googleapis.com/auth/calendar')

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

然后您的主页将确保您的用户已登录,并且@decorator.oauth_required装饰器会将OAuth 2.0令牌保存在您的数据存储区中。

class MainPage(webapp2.RequestHandler):
  @decorator.oauth_required
  def get(self):
    # This will force the user to go through OAuth
    self.response.write(...)
    # show some page to them

在您显示给他们的页面上,您可能会有POST/add-event的表单,此AddEvent处理程序将能够使用该令牌发出请求。我们不使用oauth_required而是使用@decorator.oauth_aware来实现优雅的失败。如果App Engine在请求中检测到用户的浏览器会话(如果他们POST来自表单),那么您的应用将在进行身份验证之前从数据存储中查找OAuth 2.0凭据日历请求。

class AddEvent(webapp2.RequestHandler):
  @decorator.oauth_aware
  def post(self):
    if decorator.has_credentials():          
      event_name = self.request.get('event-name')
      some_event = {...}  # Create event here
      # Documented at
      # https://developers.google.com/google-apps/calendar/v3/reference/events/insert

      http = decorator.http()
      # Using 'primary' will insert the event for the current user
      request = service.events().insert(calendarId='primary', body=some_event)
      inserted = request.execute(http=http)
      self.response.write(json.dumps(inserted))
    else:
      self.response.write(json.dumps({'error': 'No credentials'})

最后,为确保所有这些路由都有效,您需要为每个处理程序和装饰器使用的OAuth 2.0处理程序定义路由:

app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/add-event', AddEvent),
    (decorator.callback_path, decorator.callback_handler())
    ],
    debug=True)

额外参考:

https://developers.google.com/api-client-library/python/platforms/google_app_engine

https://developers.google.com/google-apps/calendar/v3/reference/events/insert

答案 1 :(得分:0)

我最近在努力整合Google日历时遇到了困难。我写了自己的文档。也许它会有所帮助:

http://www.tqis.com/eloquency/googlecalendar.htm