使用 Python apiclient 忙时调用 Google Calendar API v3 时出现 TypeError

Jor*_*alo 3 python google-app-engine google-calendar-api oauth-2.0

我想获取两个给定日期之间我的 Google 日历的所有空闲/忙碌事件。我正在关注freebusy 对象的文档

\n\n

基本上,我有一个index.html,其表单允许选择两个日期。我将这些日期发送到我的应用程序(Python Google AppEngine 支持)。

\n\n

这是经过简化的代码,使其更具可读性:

\n\n
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), \'client_secrets.json\')\n\ndecorator = oauth2decorator_from_clientsecrets(\n    CLIENT_SECRETS,\n    scope=\'https://www.googleapis.com/auth/calendar\',\n    message=MISSING_CLIENT_SECRETS_MESSAGE)\n\nservice = build(\'calendar\', \'v3\')\n\nclass MainPage(webapp2.RequestHandler):\n  @decorator.oauth_required\n  def get(self):\n    # index.html contains a form that calls my_form\n    template = jinja_enviroment.get_template("index.html")\n    self.response.out.write(template.render())\n\nclass MyRequestHandler(webapp2.RequestHandler):\n  @decorator.oauth_aware\n  def post(self):\n    if decorator.has_credentials():\n\n      # time_min and time_max are fetched from form, and processed to make them\n      # rfc3339 compliant\n      time_min = some_process(self.request.get(time_min))\n      time_max = some_process(self.request.get(time_max))\n\n      #\xc2\xa0Construct freebusy query request\'s body\n      freebusy_query = {\n        "timeMin" : time_min,\n        "timeMax" : time_max,\n        "items" :[\n          {\n            "id" : my_calendar_id\n          }\n        ]\n      }\n\n      http = decorator.http()\n      request = service.freebusy().query(freebusy_query)\n      result = request.execute(http=http)\n    else:\n      # raise error: no user credentials\n\napp = webapp2.WSGIApplication([\n    (\'/\', MainPage),     \n    (\'/my_form\', MyRequestHandler),\n    (decorator.callback_path, decorator.callback_handler())\n], debug=True)\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我在 freebusy 调用中收到此错误(堆栈跟踪的有趣部分):

\n\n
File "/Users/jorge/myapp/oauth2client/appengine.py", line 526, in setup_oauth\n    return method(request_handler, *args, **kwargs)\n  File "/Users/jorge/myapp/myapp.py", line 204, in post\n    request = service.freebusy().query(freebusy_query)\n  TypeError: method() takes exactly 1 argument (2 given)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我做了一些研究,但没有找到任何在 Python 上使用日历 v3 和 freebusy 调用的运行示例。我在API 资源管理器中成功执行了调用。

\n\n

如果我理解这个错误,似乎 oauth_aware 装饰器以任何方式过滤其控制下的代码的所有调用。可调用对象被传递给OAuthDecorator.oauth_awareoauth2client 的方法。这个可调用对象是 webapp2.RequestHandler 的一个实例。喜欢MyRequestHandler

\n\n

如果用户正确登录,则 oauth_aware 方法会通过调用 来返回对所需方法的调用method(request_handler, *args, **kwargs)。错误来了。A TypeError,因为method接受的参数多于允许的参数。

\n\n

这是我的解释,但我不知道我是否正确。我应该freebusy().query()用其他方式打电话吗?我的分析真的有道理吗?我对此迷失了......

\n\n

提前谢谢了

\n

Jor*_*alo 5

正如bossylobster所建议的,解决方案非常简单。只需替换此调用即可

service.freebusy().query(freebusy_query)
Run Code Online (Sandbox Code Playgroud)

有了这个

service.freebusy().query(body=freebusy_query)
Run Code Online (Sandbox Code Playgroud)

谢谢!