TypeError on freebusy call to Google Calendar API

2019-07-20 13:24发布

I want to get all the freebusy events of my Google Calendar between two given dates. I'm following the documentation of the freebusy object.

Basically, I have an index.html with a form that allows to choose two dates. I send those dates to my application (Python Google AppEngine backed).

This is the code, simplified, to make it more readable:

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)

But I get this error in the freebusy call (interesting part of the stack trace):

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)

I've done some research, but I didn't found any running example with calendar v3 and freebusy call on Python. I successfully executed the call in the API explorer.

If I understood the error, seems that the oauth_aware decorator filters in any way all the calls of the code under its control. A callable is passed to the method OAuthDecorator.oauth_aware of oauth2client. And this callable is an instance of webapp2.RequestHandler. Like MyRequestHandler.

If the user is properly logged, then the oauth_aware method returns a call to desired method, by calling method(request_handler, *args, **kwargs). And here comes the error. A TypeError, because method is taking more arguments than allowed.

That's my interpretation, but I don't know if I'm right. Should I call freebusy().query() in any other way? Does any piece of my analysis really make sense? I'm lost with this...

Many thanks in advance

1条回答
来,给爷笑一个
2楼-- · 2019-07-20 14:18

As bossylobster suggested, the solution was really easy. Just replace this call

service.freebusy().query(freebusy_query)

With this one

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

Thanks!

查看更多
登录 后发表回答