pmala84
pmala84

Reputation: 21

Google Calendar API Service Account Error 401 Invalid credential

I'm trying to create a Google Calendar Service Account.

Here are my credentials:

enter image description here

Here's my service account definition (Site Wide delegation for service account is enabled):

enter image description here

Here's my "Manage API client access" page:

enter image description here

And this is my code:

        string userName = "[email protected]";
        string[] Scopes = { CalendarService.Scope.Calendar, CalendarService.Scope.CalendarEvents };
        string ApplicationName = "Client per quickstart";
        ServiceAccountCredential credential = GoogleCredential.FromFile(@"path\credentialjson.json").CreateScoped(Scopes).CreateWithUser(userName).UnderlyingCredential as ServiceAccountCredential;

        // Create Google Calendar API service.
        var service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName
        });
        // THIS REQUEST FAILS!!  
        var y = service.CalendarList.List().Execute(); 

The service is created correctly but all requests fail with this error:

Code: 401, Message: "Invalid Credentials", Location: "Authorization", >LocationType: "header", Reason: "authError", Domain: "global"

I don't understand what I'm missing in the authorization header.

What should I set before the request that was not done when creating the service? Thanks!

Upvotes: 1

Views: 1078

Answers (2)

alberto vielma
alberto vielma

Reputation: 2342

Your issue is due to the Scopes you are using at the same time, because you are setting CalendarService.Scope.CalendarEvents, this one will be the priority over CalendarService.Scope.Calendar, because the Google APIs always take as priority the most restricted Scope (in this case CalendarEvents).

And as you can see in the CalendarList: list endpoint scopes, the CalendarService.Scope.CalendarEvents is not a Scope you could use to hit the endpoint you wanted to hit.

So, in your code you can delete the extra Scope and leave it like this:

string[] Scopes = { CalendarService.Scope.Calendar };

Upvotes: 1

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

Do you see the text over the top of your credentials

OAuth 2.0 Client IDs

Go to google developer console click + create credentials pick service account from the drop down. You need to ensure that you have downloaded the key fine for the Service account and not for the Oauth2 client. The message you are seeing states that you have the wrong client type. Try and download the service account key file again.

example of service account credentials

enter image description here

You have created oauth2 credentials not service account creditnals.

GoogleCredential credential;
                    using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
                    {
                        credential = GoogleCredential.FromStream(stream)
                             .CreateScoped(scopes);
                    }

                    // Create the  Analytics service.
                    return new CalendarService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "Calendar Service account Authentication Sample",
                    });

Upvotes: 0

Related Questions