【问题标题】:TypeError on freebusy call to Google Calendar API v3 using Python apiclient使用 Python apiclient 对 Google Calendar API v3 进行空闲忙碌调用时出现 TypeError
【发布时间】:2012-12-06 06:29:05
【问题描述】:

我想在两个给定日期之间获取我的 Google 日历的所有闲忙事件。我正在关注documentation of the freebusy object

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

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

CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

decorator = oauth2decorator_from_clientsecrets(
    CLIENT_SECRETS,
    scope='https://www.googleapis.com/auth/calendar',
    message=MISSING_CLIENT_SECRETS_MESSAGE)

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

class MainPage(webapp2.RequestHandler):
  @decorator.oauth_required
  def get(self):
    # index.html contains a form that calls my_form
    template = jinja_enviroment.get_template("index.html")
    self.response.out.write(template.render())

class MyRequestHandler(webapp2.RequestHandler):
  @decorator.oauth_aware
  def post(self):
    if decorator.has_credentials():

      # time_min and time_max are fetched from form, and processed to make them
      # rfc3339 compliant
      time_min = some_process(self.request.get(time_min))
      time_max = some_process(self.request.get(time_max))

      # Construct freebusy query request's body
      freebusy_query = {
        "timeMin" : time_min,
        "timeMax" : time_max,
        "items" :[
          {
            "id" : my_calendar_id
          }
        ]
      }

      http = decorator.http()
      request = service.freebusy().query(freebusy_query)
      result = request.execute(http=http)
    else:
      # raise error: no user credentials

app = webapp2.WSGIApplication([
    ('/', MainPage),     
    ('/my_form', MyRequestHandler),
    (decorator.callback_path, decorator.callback_handler())
], debug=True)

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

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

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

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

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

这是我的解释,但我不知道我是否正确。我应该以其他方式致电freebusy().query() 吗?我的任何分析真的有意义吗?我迷路了……

在此先感谢

【问题讨论】:

  • 你试过service.freebusy().query(body=freebusy_query)吗?
  • 有效!我应该添加回复,给你信用,还是你想发布它?
  • 足够好。如果你想表达爱意,你可以随时支持我的评论,但这没什么大不了的。

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


【解决方案1】:

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

service.freebusy().query(freebusy_query)

有了这个

service.freebusy().query(body=freebusy_query)

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多