我想在兩個給定日期之間獲取我的Google日曆的所有freebusy事件。我正在關注documentation of the freebusy object。使用Python apiclient freebusy調用Google Calendar API v3時的TypeError
基本上,我有一個表格,允許選擇兩個日期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
。
如果用戶正確登錄,則通過調用method(request_handler, *args, **kwargs)
,oauth_aware方法返回所需方法的調用。這裏出現錯誤。 A TypeError
,因爲method
正在接受比允許更多的參數。
這是我的解釋,但我不知道我是否正確。我應該用其他方式撥打freebusy().query()
嗎?我的分析是否有意義?我提前
你試過'service.freebusy()。query(body = freebusy_query)'? – bossylobster
它的工作原理!我應該添加回復,給你信用,還是你想發佈它? – jorgeas80
夠好。如果你想展示愛情,你總是可以提出我的評論,但這並不是什麼大不了的事情。 – bossylobster