簡體   English   中英

Google Calendar API C#憑據引發異常

[英]Google Calendar API C# credentials throw an exception

我正在遵循本教程: https : //developers.google.com/google-apps/calendar/instantiate我的代碼:

public static void Main()
        {
            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets()
                {
                    ClientId = "actualclientid",
                    ClientSecret = "actualclientsecret"
                },
                new[] { CalendarService.Scope.Calendar },
                "user",
                CancellationToken.None).Result;

問題是,這部分代碼使我拋出異常:

{“無法加載文件或程序集'Microsoft.Threading.Tasks.Extensions.Desktop,版本= 1.0.16.0,Culture =中性,PublicKeyToken = b03f5f7f11d50a3a'或其依賴項之一。系統找不到指定的文件。”: Microsoft.Threading.Tasks.Extensions.Desktop,版本= 1.0.16.0,區域性=中立,PublicKeyToken = b03f5f7f11d50a3a“}

PS .:雖然很可能與該問題無關,但是在Google上設置我的項目以獲取憑據時,我選擇了Installed Application-> Other(因為我假設這就是控制台應用程序)

編輯:添加https://www.nuget.org/packages/Microsoft.Bcl.Async/1.0.166-beta似乎已經解決了該問題。 現在剩下的代碼:

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

    var x = service.Events.List("actualcalendarid").OauthToken;

    Console.WriteLine(x);
    Console.ReadLine();

返回一個空行,即使我運行該應用程序時它確實請求訪問我的日歷等。 我忘記了什么嗎?

確保已將所有引用添加到項目中。

需要使用NuGet安裝以下組件

-Install-Package Google.Apis.Calendar.v3 –Pre
-Install-Package Google.Apis -Pre
-Install-Package DotNetOpenAuth -Version 4.3.4.13329
-Install-Package Google.Apis.Auth.Mvc -Pre

並將引用Microsoft.Threading.Tasks.Extensions添加到您的項目。

編輯:您不檢索數據,因為您不執行查詢。

var result = service.Events.List("primary").Execute();


    private async Task<UserCredential> GetCredential()
    {
        UserCredential credential = null;
        try
        {
            using(var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { CalendarService.Scope.Calendar },
                    "user", CancellationToken.None, new FileDataStore(""));
            }
        }
        catch (IOException ioe)
        {
            Debug.WriteLine("IOException" + ioe.Message);
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Exception" + ex);
        }
        return credential;
    }

    private CalendarService GetCalenderService(UserCredential credential)
    {
        CalendarService service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Calendar Sample"
            });
        return service;
    }

    private Events GetEvent(string CalendarId)
    {
        var query = this.Service.Events.List(CalendarId);
        query.ShowDeleted = false;
        query.SingleEvents = true;
        return query.Execute();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM