Python GAE访问日历API V3中的OAuth2身份验证(域托管)
我正在用Python开发一个Google应用程序引擎应用程序。我用的是:Python GAE访问日历API V3中的OAuth2身份验证(域托管),python,google-app-engine,oauth-2.0,google-calendar-api,google-api-python-client,Python,Google App Engine,Oauth 2.0,Google Calendar Api,Google Api Python Client,我正在用Python开发一个Google应用程序引擎应用程序。我用的是: 谷歌日历API v3(用于访问我自己域中的日历。因此,这是安装在我域中的谷歌应用程序) Google API Python客户端库 OAuth2对我的域的用户进行身份验证(name@mydomain.com) 我认为我必须使用服务帐户,因为: “如果您的应用程序引擎应用程序需要调用API来访问应用程序项目拥有的数据,您可以使用服务帐户简化OAuth 2.0” 取自 但我不确定我是否误解了什么。我的场景(GAE应用程序试
- 谷歌日历API v3(用于访问我自己域中的日历。因此,这是安装在我域中的谷歌应用程序)
- Google API Python客户端库
- OAuth2对我的域的用户进行身份验证(name@mydomain.com)
- 如前所述,使用服务帐户
- 使用Google API客户端库为Python提供的Python装饰器(OAuth2Decorator和OAuth2DecoratorFromClientSecrets)
- 在我的本地计算机中执行:HttpError 401请求时返回“无效凭据”(我将事件创建为JSON对象,如下所示:)。错误堆栈跟踪:
- 部署到GAE:Error 310(网络::ERR\u太多\u重定向)。字符串“\u ah/login\u required?continue=”多次附加在url的末尾。 可能是客户端ID/客户端机密或API控制台生成的服务帐户参数有问题?我应该重新创建它们吗
非常感谢您,您不需要服务帐户,尽管使用服务帐户可能会很有用。App Engine上的服务帐户存在一些棘手的问题,详细信息请参见库中的一篇文章。试着玩弄一下,看看这是否有助于澄清如何使用API 只要你用一个可以访问这些日历的帐户来授权应用程序,你就可以访问它们,不管它是否在Google App Engine上 在这里使用
OAuth2Decorator
是最好的选择。如果你给出一个具体的例子,我很乐意提供一些代码片段来完成这项任务
请参阅最近提出的一个类似问题:这似乎是您的用例,只是您希望使用日历API而不是驱动器API
更新:
读完你(我会考虑关闭,如果我是你),我已经拼凑了一个样本,可以帮助你理解如何使用装饰器。
首先,要使用您的凭据,以便您的应用程序可以让用户对其进行授权:
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\u required
decorator将在数据存储中保存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
s发布到/add event
,并且该AddEvent
处理程序将能够使用令牌发出请求。我们使用@decorator.oauth\u aware
来允许优雅的失败,而不是使用oauth\u required
。如果应用程序引擎cookie从其浏览器会话(如果他们从表单中发布)的请求中检测到用户,则在发出经过身份验证的日历请求之前,应用程序将从数据存储中查找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)
额外参考:
反响很好!非常感谢。我把我的代码缩减到了最低限度。现在,我有更多的问题,但我会提出其他问题。再次感谢!
decorator.callback\u path
和decorator\u callback\u handler()
是否确实存在于您的appengine.py
版本中?我有一个类似的问题,我正在努力处理这些回调参数,我可以请你看一下吗?