使用Python apiclient在freebusy上调用Google Calendar API v3时的TypeError [英] TypeError on freebusy call to Google Calendar API v3 using Python apiclient

查看:103
本文介绍了使用Python apiclient在freebusy上调用Google Calendar API v3时的TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两个给定日期之间获得我的Google日历的所有freebusy事件。我遵循文档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 =' ('calendar','v3')

class MainPage(webapp2.RequestHandler):
@ decorator.oauth_required
def get(self):
#index.html包含一个调用my_form
template = jinja_enviroment.get_template (index.html)
self.response.out.write(template.render())
$ b $ class MyRequestHandler(webapp2.RequestHandler):
@ decorator.oauth_aware
def post(self):
如果decorator.has_credentials():

#time_min和time_max从窗体中获取,并进行处理以使它们符合
#rfc3339符合
time_min = some_process(self.request.get(time_min))
time_max = some_process(self.request.get(time_max))

#构建freebusy查询请求的正文
freebusy_query = {
timeMin:time_min,
timeMax:time_max,
items:[
{
id:my_calendar_id
}


$ b $ http = decorator.http()
request = service.freebusy()。query(freebusy_query)
result = request.execute( http = http)
其他:
#引发错误:无用户凭证

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

但是我在freebusy调用中遇到这个错误(有趣的堆栈跟踪部分):

pre > 文件/ Users / jorge / myapp / oauth2client /appengine.py,第526行,在setup_oauth
返回方法(request_handler,* args,** kwargs)
文件/Users/jorge/myapp/myapp.py,第204行,发布
request = service.freebusy()。query(freebusy_query)
TypeError:method()只需要1个参数(给出2个)

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

如果我理解错误,似乎oauth_aware修饰符以任何方式过滤在其控制下的所有代码调用。可调用函数被传递给oauth2client的方法 OAuthDecorator.oauth_aware 。这个可调用是webapp2.RequestHandler的一个实例。像 MyRequestHandler



如果用户被正确记录,那么oauth_aware方法会返回所需方法的调用,调用方法(request_handler,* args,** kwargs)。这里出现错误。 A TypeError ,因为方法的参数比允许的更多。



这是我的解释,但我不知道我是否正确。我应该以任何其他方式调用 freebusy()。query()吗?我的分析是否有意义?我失去了这个...



非常感谢提前

解决方案

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

  service.freebusy()。query(freebusy_query)

使用这一个

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

谢谢!


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

解决方案

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!

这篇关于使用Python apiclient在freebusy上调用Google Calendar API v3时的TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆