简体   繁体   中英

Google Calendar API C# credentials throw an exception

I'm following this tutorial: https://developers.google.com/google-apps/calendar/instantiate My code:

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

The issue is, this part of the code throws me an exception:

{"Could not load file or assembly 'Microsoft.Threading.Tasks.Extensions.Desktop, Version=1.0.16.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.":"Microsoft.Threading.Tasks.Extensions.Desktop, Version=1.0.16.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"}

PS.: While most likely irrelevant to the issue, when setting up my project on google to get the credentials, I've selected Installed Application -> Other (As I'm assuming that's what a console application is)

EDIT: Adding https://www.nuget.org/packages/Microsoft.Bcl.Async/1.0.166-beta seems to have solved that issue. Now the remained of my code:

 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();

returns an empty line, even though when I run the application it does request access to my calendar and such. Am I forgetting something?

Make sure you have added all the references to your project.

The following need to be installed using 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

And add reference Microsoft.Threading.Tasks.Extensions to your project.

EDIT: You don't retrieve data because you are not executing your query.

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM