2013-03-02 116 views
0

我正在使用Google Calendar API在Google App Engine应用程序中创建单个事件事件。Google Calendar API创建事件返回201,但未创建任何事件

的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事件。

但事件没有创建,它不会显示在我的日历上。当我在返回的事件数据中的“备用链接”字段中输入网址时,我的日历中会显示一条错误消息,提示“事件不存在”

任何关于此问题的帮助都将非常感激。我对Google的API非常陌生,所以希望我错过了一些明显的东西。

+0

从来没有使用API​​,但你知道'200'是HTTP状态 - 确定的响应?我无法想象谷歌会摆脱这一点。 – 2013-03-02 18:57:45

+1

@NiklasR根据[RFC2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)201状态代码是“请求已完成并导致创建新资源。” 从Calendar API的[procotol guide](https://developers.google.com/google-apps/calendar/v2/developers_guide_protocol#AuthOAuth): >当您发送第二个POST请求(或第一个POST请求没有重定向的情况下),Calendar创建一个日历事件,然后返回一个HTTP 201 CREATED状态代码,以及一个元素或(在JSON-C)数据对象形式的新事件的副本。 – pointlessCog 2013-03-02 22:20:05

回答

0

得到它的工作。根据协议指南日历API:

要发布的条目,发送以下HTTP请求到日历,使用一种特殊的“默认” URL(和授权报头;参见上认证的部分上方)。日历会自动将默认URL重定向到属于已验证用户的日历的读/写私人提要的URL。 (请注意,您不必使用默认URL向日历发送POST请求;如果您愿意,可以指定用户ID而不是“默认”。有关详细信息,请参阅日历Feed类型参考。)

POST https://www.google.com/calendar/feeds/default/private/full 

而这确实返回了201响应,但没有添加事件。但是,当我为默认用户指定用户ID时,它就起作用了。

POST https://www.google.com/calendar/feeds/[email protected]/private/full 

我还不确定为什么默认提要不起作用。

+0

原来我最大的问题是没有阅读正确的文档!虽然上面的工作,它是从API的V2,我们应该使用V3。 – pointlessCog 2013-03-04 11:18:04

+0

如果您想简要介绍Google Calendar API v3,请查看以下链接:https://google-api-client-libraries.appspot.com/documentation/calendar/v3/python/latest/ – Dev 2013-03-04 12:03:23