我想在两个给定日期之间获取我的谷歌日历的所有空闲事件。我正在关注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 装饰器以任何方式过滤了其控制下的代码的所有调用。一个可调用对象被传递给OAuthDecorator.oauth_aware
oauth2client 的方法。而这个可调用对象是 webapp2.RequestHandler 的一个实例。喜欢MyRequestHandler
。
如果用户已正确登录,则 oauth_aware 方法通过调用method(request_handler, *args, **kwargs)
. 错误来了。A TypeError
, 因为method
正在接受比允许更多的参数。
这是我的解释,但我不知道我是否正确。我应该freebusy().query()
用其他方式打电话吗?我的任何分析真的有意义吗?我迷失了这个...
提前谢谢了