Martin Nazakov
Martin Nazakov

Reputation: 5

Google Calendar API in ASP.NET MVC

So, I spent a ridiculous amount of time googling about this issue. Basically, this is my auth code:

        UserCredential credential;

        using (var stream =
            new FileStream(@"C:\Users\Marto\Desktop\project\MVC_2b\App_Data\client_secret.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart");

            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 = ApplicationName,
            ApiKey = "key",
        });

And this is where the error occurs: (error 401 on Execute())

Calendar calendar=new Calendar();
        calendar.Id = "Test";
        service.Calendars.Insert(calendar).Execute();

This is my error:

An exception of type 'Google.GoogleApiException' occurred in Google.Apis.dll but was not handled in user code

Additional information:

Google.Apis.Requests.RequestError - Invalid Credentials [401]

Errors [Message[Invalid Credentials] Location[Authorization - header] Reason[authError] Domain[global]]

I am not sure if I am not doing something right with the authorization tho.

Any help would be appreciated, Thank you!

Upvotes: 0

Views: 2433

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

Well that was a little bit of a pain but i got it working.

You didnt post what scope you where sending i am using

CalendarService.Scope.Calendar

Build the new calendar

var NewCalendar = new Google.Apis.Calendar.v3.Data.Calendar();
NewCalendar.Summary = "My New Calendar";   // this is the title of the new calendar

Note You cant send ID this is something Google creates

Send the request

var request = service.Calendars.Insert(NewCalendar );              
var myCalendar = request.Execute();

Upvotes: 1

Related Questions