Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python GAE访问日历API V3中的OAuth2身份验证(域托管)_Python_Google App Engine_Oauth 2.0_Google Calendar Api_Google Api Python Client - Fatal编程技术网

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

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应用程序试

我正在用Python开发一个Google应用程序引擎应用程序。我用的是:

  • 谷歌日历API v3(用于访问我自己域中的日历。因此,这是安装在我域中的谷歌应用程序)
  • Google API Python客户端库
  • OAuth2对我的域的用户进行身份验证(name@mydomain.com)
我认为我必须使用服务帐户,因为:

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

取自

但我不确定我是否误解了什么。我的场景(GAE应用程序试图在我自己的域中访问谷歌应用程序)是服务帐户的候选方案吗

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

  • 如前所述,使用服务帐户
  • 使用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
版本中?我有一个类似的问题,我正在努力处理这些回调参数,我可以请你看一下吗?