2013-07-25 78 views
5

我試圖使用Google日曆API v3創建日曆,如果它尚不存在。我的實現成功檢索我的所有日​​歷和事件,並可以更改日曆,但我很努力添加新的日曆。這是我爲了嘗試添加一個新的日曆爲用戶做到:使用c#.net創建Google日曆Api v3的日曆時出現404錯誤

... 
var calendarService = new CalendarService(_authenticator); 
var calendarId = "com.somedomain.somename.1234"; // Some random ID 
try { 
    calendarManager.GetCalendar(calendarId); // No calandar found 
} catch(GoogleCalandarServiceProxyException e){ 
    // Ok, so far so good, calendar not found (that is correct) and I am here 
    var someCalendar = new Google.Apis.Calendar.v3.Data.CalendarListEntry { 
     Summary = "Some summary!", 
     Description = "A calendar descr.", 
     AccessRole = "writer", 
     BackgroundColor = "#E7323C", 
     Id = calendarId 
    }; 
    calendarService.CalendarList.Insert(someCalendar).Fetch(); // Fetch to execute 
} 

請求生成:

insert @ https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json&prettyPrint=true 

RequestError:

Google.Apis.Request.RequestErrorNotFound[404]Errors [Message[Not Found]Location[-] Reason[notFound] Domain[global]]] 

奇怪的事情是在做一個GET在https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json&prettyPrint=true不返回東西,所以它應該在那裏。

我希望有一些很棒的人/女孩在那裏知道該怎麼做!我完全被卡住了。

更新 好像我收到的CalendarList谷歌阿比瀏覽器也是同樣的404錯誤:

地址https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert

請求

POST https://www.googleapis.com/calendar/v3/users/me/calendarList?key={YOUR_API_KEY} 

Content-Type: application/json 
Authorization: Bearer ya29.AHES6ZQTj-D6uIMOWr_AoFYVvfefsEGcVvLvvMf4aKhgrdvQE25aVw 
X-JavaScript-User-Agent: Google APIs Explorer 

{ 
"id": "hastalavista" 
} 

迴應

404 Not Found 

- Show headers - { "error": { "errors": [ { 
    "domain": "global", 
    "reason": "notFound", 
    "message": "Not Found" } ], "code": 404, "message": "Not Found" } } 

有誰知道Google是否已更改此資源的URL?如果是,我如何更改Google Calendar Api v3以使用更新後的url ...?

回答

10

歐凱,是我不好:

您不能創建自己的標識日曆,CalendarListEntry.Insert()的目的是插入一個創建日曆進入日曆

所以名單創建一個新的日曆一個有:

  1. 插入一個新的日曆:https://developers.google.com/google-apps/calendar/v3/reference/calendars/insert

    (不添加id作爲paramater,這將自動創建)

  2. 插入在步驟1中創建日曆的ID,並將其插入到calendarList:https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert

1

我還是完全由於數據似乎重疊,所以對Google日曆和CalendarList之間的區別感到困惑。爲什麼日曆被視爲「次要」日曆。

0

下一個代碼是我的詭計,提到的參考是Java而不是。網絡和往常一樣療法e爲一些是煩人的差異...

私人布爾createCalendar(){

 // use Google.Apis.Calendar 
     Calendar calendar = new Calendar(); 
     calendar.Summary = "MyNewCalendar"; 
     calendar.Description = "Your new Calendar"; 
     calendar.Location = "Aadorp"; 
     calendar.TimeZone = "Europe/Amsterdam"; 

     try 
     { 
      // Not inserting in CalendarList but in Calendar! 
      calendarService.Calendars.Insert(calendar).Execute(); 
     } 
     catch (Exception exception) 
     { 

      MessageBox.Show(exception.Message); 
      return false; 
     } 
     return true; 
    }