Google日历API创建事件返回201但没有创建事件
我正在使用谷歌日历的API,从我的谷歌应用引擎应用中创建一个单次事件。
这个用JSON格式编码的事件看起来是这样的:
event = {"data":{
"title": "Test Event",
"details": "Details of test event",
"transparency": "opaque",
"status": "confirmed",
"location": "My Place",
"when": [{
"start": "2013-03-05T15:00:00.000Z",
"end": "2013-03-05T17:00:00.000Z"
}]
}}
我用来添加事件的代码是:
def sendGCalEvent(self, event):
scope = "https://www.google.com/calendar/feeds/"
authorization_token, _ = app_identity.get_access_token(scope)
logging.info("Using token %s to represent identity %s",
authorization_token, app_identity.get_service_account_name())
payload = simplejson.dumps(event)
response = urlfetch.fetch(
"https://www.google.com/calendar/feeds/default/private/full",
method=urlfetch.POST,
payload=payload,
headers = {"Content-Type": "application/json",
"Authorization": "OAuth " + authorization_token})
if response.status_code == 201:
result = simplejson.loads(response.content)
logging.info("Status code was %s, and the returned event looks like %s", response.status_code, result)
return
raise Exception("Call failed. Status code %s. Body %s",
response.status_code, response.content)
一切看起来都正常,认证也没问题,事件成功发布,我收到了201的响应,还有包含日历中添加字段的事件JSON。
但是事件并没有被创建,它在我的日历上不显示。当我去查看返回事件数据中的“备用链接”字段里的网址时,我的日历上出现了一个错误信息,说“事件不存在”。
如果有人能帮忙,我会非常感激。我对谷歌的API还很陌生,希望我只是遗漏了一些明显的东西。
1 个回答
0
我搞定了。根据日历API的协议指南:
要添加一个条目,你需要发送一个HTTP请求到日历,使用一个特别的“默认”网址(还需要一个授权头;具体的认证部分可以参考上面的内容)。日历会自动把这个默认网址转到已认证用户的私有读写日历的网址。(注意,你不一定要使用默认网址来发送POST请求到日历;如果你愿意,可以用用户ID来替代“默认”。想了解更多,可以查看日历的不同数据源类型。)
POST https://www.google.com/calendar/feeds/default/private/full
这样做确实返回了201的响应,但并没有添加事件。不过,当我指定了默认用户的用户ID时,它就能正常工作了。
POST https://www.google.com/calendar/feeds/user@website.com/private/full
我还是不太明白为什么默认的数据源不管用。