2014-05-12 32 views
1

我正在使用Google Calendar API V3和OAuth2 & .NET。 我的身份驗證是由服務帳戶,因爲我需要運行它作爲服務,沒有用戶界面。 我經過了很多努力來驗證憑據,但由於某種原因,我無法在日曆上創建活動(&是的,我與我分享了它...)。使用服務器帳戶和.NET進行的使用Google Calendar API的身份驗證

我發現了很多關於某些相同問題的問題,但都在php中,我不知道或認爲它會幫助我。

我尋求一些幫助。謝謝

String serviceAccountEmail = "[email protected]"; 

      var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.MachineKeySet |X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); 
      ServiceAccountCredential credential = new ServiceAccountCredential(
       new ServiceAccountCredential.Initializer(serviceAccountEmail) 
       { 
        Scopes = new[] { CalendarService.Scope.Calendar } 
       }.FromCertificate(certificate)); 

      // Create the service. 
      var service = new CalendarService(new BaseClientService.Initializer() 
      {      
       HttpClientInitializer = credential, 
       ApplicationName = "Calendar API Sample", 
      }); 

      Event e = new Event() 
      { 
       Summary = "Appointment1", 
       Location = "42.138679, -88.045519", 
       Start = new EventDateTime() 
       { 
        DateTime = DateTime.Now, 
        TimeZone = "Asia/Jerusalem" 
       }, 
       End = new EventDateTime() 
       { 
        DateTime = DateTime.Now.AddDays(1), 
        TimeZone = "Asia/Jerusalem" 
       }, 
      }; 

      Event createdEvent = service.Events.Insert(e, "primary").Execute();     
+1

什麼是錯誤? – peleyal

+0

我一直在努力與谷歌.NET庫以及。 API看起來不錯,但它只是一些問題1)每個版本似乎完全打破了最新版本,並且2)沒有好的示例應用程序,只是開發人員需要拼湊在一起的代碼片段。 – Craig

+0

1.圖書館是BETA時發生了很大的變化。但是,現在已經達到了GA,不會有任何不兼容的變化。 2. 我們的示例存儲庫中有少量樣本 - https://code.google.com/p/google-api-dotnet-client/source/browse?repo=samples。請記住,這是一個開源項目,所以如果你將來會共享一個樣本,它將會很棒。 – peleyal

回答

0

由於某些原因,插入事件到「主」日曆,沒有做這項工作。 相反,我寫了下面的代碼,它允許我寫這個事件。

var list = service.CalendarList.List().Execute().Items; 
service.Events.Insert(e, list[0].Id).Execute(); 

這是我對這個問題的解決方案,我也同意克雷格這裏的API沒有很好的組織。 (在使用地圖的令人驚歎的API後說這些)。

0

也許嘗試以編程方式指定用戶,而不是與您的服務帳戶電子郵件共享日曆?以下在我的應用程序中工作:

ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(serviceAccountEmail) 
    { 
     User = "[email protected]", 
     Scopes = new[] { CalendarService.Scope.Calendar } 
    }.FromCertificate(certificate)); 

// ... Define the event  

service.Events.Insert(e, "primary").Execute(); 
相關問題