1

我正在使用谷歌日曆api服務來訪問用戶日曆,它在我的本地工作正常,但它不能在服務器下工作是在我的本地工作正常的代碼。如何解決使用谷歌日曆api時發生的「一個或多個錯誤」問題

public ActionResult AddGoogleEvent() 
{   
    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
     new ClientSecrets 
     { 
      ClientId = clientid, 
      ClientSecret = clientsecret, 
     }, 
     new[] { CalendarService.Scope.Calendar }, 
     "user", 
     CancellationToken.None).Result; 

    var service = new CalendarService(new BaseClientService.Initializer(){ 
     HttpClientInitializer = credential, 
     ApplicationName = "sampleappilication" 
     }); 

    Google.Apis.Calendar.v3.Data.Event event1 = new Google.Apis.Calendar.v3.Data.Event() 
    { 
     Summary = "Appointment", 
     Location = location, 
     Start = new Google.Apis.Calendar.v3.Data.EventDateTime() 
     { 
      DateTime = new DateTime(Convert.ToInt32(yy), Convert.ToInt32(mn), Convert.ToInt32(dy), Convert.ToInt32(sthour), Convert.ToInt32(stminute), 0), 
      TimeZone = location 
     }, 
     End = new Google.Apis.Calendar.v3.Data.EventDateTime() 
     { 
      DateTime = new DateTime(Convert.ToInt32(yy), Convert.ToInt32(mn), Convert.ToInt32(dy), Convert.ToInt32(ethour), Convert.ToInt32(etminute), 0), 
      TimeZone = "America/Los_Angeles" 
     }, 
     Recurrence = new String[] {"RRULE:FREQ=WEEKLY;BYDAY=MO"}, 
     Attendees = new List<Google.Apis.Calendar.v3.Data.EventAttendee>() 
     { 
      new Google.Apis.Calendar.v3.Data.EventAttendee() { Email = attendencess } 
     } 
    }; 

    Google.Apis.Calendar.v3.Data.Event thisevent = service.Events.Insert(event1, "primary").Execute(); // Another error. "Does not contain a definition for Fetch" 

    string newEventID = thisevent.Id; 

    Session["Accepted"] = "Accepted"; 
    return RedirectToAction("Eventconfirm"); 
} 

異常

訪問路徑 'Google.Apis.Auth' 被拒絕。堆棧跟蹤:在 Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSu CCESS(Tasktask)自性rosoft.Runtime.Compi lerServices.TaskAwai ter.HandleNonSuccess(任務 任務)在 Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker .d__1.MoveNe xt() in C:\ Users \ mdril \ Documents \ GitHub \ google-api-dotnet-client \ Src \ GoogleApis.Auth.Dot Net4 \ OAuth2 \ GoogleWe bAuthorizationBroker .cs:線 59來源:Microsoft.Threading.Tasks TargetSite:虛空 ThrowForNonSuccess(System.Threading.Tasks.Task)

任何一個可以建議如何解決這個問題

+1

「不工作」非常模糊 - 到底發生了什麼問題,以及在哪裏? (我希望你有一個異常堆棧跟蹤...) –

+0

是在檢查日誌文件時我收到以下異常。 發生一個或多個錯誤。 StackTrace:在System.Threading.Tasks.Task'1.GetResultCore(布爾waitCompletionNotification) at O365_APIs_Start_ASPNET_MVC.Controllers.ICalendarController。 d__104.MoveNext()in D:\ O365Calender_App_New \ O365Calender_App \ O365-APIs-Start-ASPNET -MVC \ \控制器ICalendarController。cs:line 4678 來源:mscorlib TargetSite:TResult GetResultCore(布爾型) --------------------------------- -------------------------- –

+0

首先,這應該是在問題中,而不是在評論 - 然後,你應該找到*真實* InnerExceptions中的原始異常。如果沒有這些信息,它將非常難以幫助你。 –

回答

1
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
     new ClientSecrets 
     { 
      ClientId = clientid, 
      ClientSecret = clientsecret, 
     }, 
     new[] { CalendarService.Scope.Calendar }, 
     "user", 
     CancellationToken.None).Result; 

默認情況下,該代碼使用FileDataStore存儲您的憑證。 FileDataStore默認將憑據存儲在%appData%中。我對filedatastore教程可以發現here

訪問路徑「Google.Apis.Auth」被拒絕

可能意味着您的服務器不能訪問寫入該路徑。你可以這樣做來改變證書的存儲路徑。

UserCredential credential; 
using (var stream = new FileStream(clientSecretsJsonFilePath 
           ,FileMode.Open 
           ,FileAccess.Read)) 
    { 
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(stream).Secrets, 
    new[] { CalendarService.Scope.Calendar }, 
    "LookIAmAUniqueUser", 
    CancellationToken.None, 
    new FileDataStore(@"c:\datastore",true)        
).Result; 
    } 

另外,您也可以創建自己的實現的idatastore和保存憑證你喜歡怎麼過

+0

嗨Dalm, 我得到DriveService.ScopeDrive的問題,其中我得到的名稱空間 –

+0

您不需要它你正在使用谷歌日曆我只是複製代碼,你可以修復它。我已經爲你做了。 – DaImTo

+0

感謝Dalm,它對我的​​作品 –