![](/img/trans.png)
[英]c# Google Calendar API V3 is ignoring my other account (multiple account authorization)
[英]Google calendar api v3 authorization and authentication with C#
您好,我找不到Google日歷api v3授權和身份驗證的工作代碼(C#)? 看起來所有帖子都太舊了,並且庫進行了更改,有人可以使用代碼嗎? 我向Google開發人員注冊了項目,並獲得了使用憑據。 謝謝。
這是一種靜態方法,用於獲取用於執行所有請求的CalendarService
對象,您需要將從Google Developers Dashboard下載的JSON文件存儲在項目中的某個位置,然后在下面的代碼中引用它。
首次運行此程序時,您將被重定向到網絡瀏覽器中的Google登錄屏幕,並被提示在此處訪問日歷。 確認后,下面將創建一個憑據文件,該文件將存儲在用戶計算機上指定的位置(例如,應用程序的安裝目錄)。 從那時起,將使用存儲的憑證文件,並且您無需再次登錄。
public static CalendarService GetService()
{
var Scopes = new string[] { CalendarService.Scope.Calendar };
UserCredential credential;
string filePath = Path.Combine("*YOUR DOWNLOADED JSON FILE LOCATION HERE*");
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
string credPath = "*WHERE YOU WILL STORE YOUR CREDENTIALS FILE*";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "*YOUR APPLICATION NAME HERE*",
});
return service;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.