【发布时间】:2013-03-02 16:32:25
【问题描述】:
我正在使用 Google 日历 API 从我的 Google App Engine 应用程序中创建单次事件。
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。
但是该活动没有创建,它没有显示在我的日历上。当我转到返回的事件数据中“备用链接”字段中的 URL 时,我的日历会出现一条错误消息,指出“事件不存在”
对此的任何帮助将不胜感激。我对 Google 的 API 很陌生,所以希望我遗漏了一些明显的东西。
【问题讨论】:
-
没用过这个API,但是你知道
200是HTTP status-ok响应吗?我无法想象 Google 会偏离这一点。 -
@NiklasR 根据RFC2616 201 状态代码是“请求已完成并导致创建新资源。”来自 Calendar API 的 procotol guide: > 当您发送第二个 POST 请求(或在没有重定向的情况下为第一个)时,Calendar 会创建一个日历事件,然后返回一个 HTTP 201 CREATED 状态代码以及一个副本
元素或(在 JSON-C 中)数据对象形式的新事件。
标签: python google-app-engine google-api google-calendar-api