2014-06-05 26 views
6

編輯:樓主問這個爲C#,但無論使用的庫出現同樣的問題,它的解決方案是語言無關谷歌日曆API CalendarList列表返回空元素

使用C#的lib,

 string service_account = "[email protected]"; 

     var certificate = new X509Certificate2(@"pathtomy-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable); 

     ServiceAccountCredential credential = new ServiceAccountCredential(
      new ServiceAccountCredential.Initializer(service_account) 
      { 
       Scopes = new[] { CalendarService.Scope.Calendar } 
      }.FromCertificate(certificate)); 


     // Create the service. 
     var service = new CalendarService(new BaseClientService.Initializer() 
     { 
      HttpClientInitializer = credential, 
      ApplicationName = "My project name", 

     }); 

     var calendarList = service.CalendarList.List().Execute(); 
     IList<CalendarListEntry> items = calendarList.Items; 

項目是空的。在https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list上,當我嘗試api時,我得到了很好的結果。

我真的不明白爲什麼我沒有任何結果:似乎如果service_account沒有與我的gmail帳戶鏈接到相同的日曆。

有什麼建議嗎? 謝謝。

回答

10

的解決方案是與服務帳戶,共享現有日曆

<paste-your-account-here>@developer.gserviceaccount.com

(你可以找到在谷歌開發者控制檯的credentials選項卡下的問題賬戶,這就是所謂的「電子郵件地址」)

+0

@aldanux - 你在說什麼?這完全是**問題的解決方案。 – mac

+0

@Rototo - 非常感謝,一小時之後我就陷入了困境,不知道下一步該怎麼辦!您保存了我的一天...... :) – mac

+0

與服務帳戶共享日曆後,日曆列表對我來說仍然顯示爲空。在與服務帳戶共享第二個日曆之後,兩者都出現了。在碰到這個解決方案之前,我差點把頭髮拉出來,所以如果你仍然陷入困境,也許會有所幫助。 – alex83