0

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!

1
  • what does your View look like? Commented Apr 3, 2016 at 21:05

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That was really what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.