1
votes
  1. We passed the verification to start using calendar.events.readonly scope on breess.com service.

  2. We created the service account with wide delegation

  3. In G Suite domain’s admin console added scope https://www.googleapis.com/auth/calendar.events.readonly

  4. The user account [email protected] granted access to his calendar

Everything looks configured. The code below is executed on the host and returns the error:

Error:"unauthorized_client", Description:"Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.", Uri:""

Have we missed something? Could you please guide us to resolve the issue?

Extra note:

var credential = GoogleCredential
.FromFile("breess-7798c393d5b0.json")
.CreateScoped(new[] { CalendarService.Scope.CalendarEventsReadonly })
.createwithuser("[email protected]");
var calendarService = new CalendarService(new     BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "BREESS" });
var request = calendarService.Events.List(calendarId);
var events = await request.ExecuteAsync();
1
Does the client id from the credentials correspond with the unique id from the service account?ale13

1 Answers

0
votes

Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.

There are three ways of authorizing Oauth with google. They each have their own type of credential file and code needed to use them.

  1. Installed application
  2. Web application
  3. Service account application
  4. API key for public access only

When you create the project on Google developer console you need to be sure that you have created service account credentials. Then you need to be sure that you are using the code that was intended for use with service account credentials

var scopes = new[] { CalendarService.Scope.CalendarEventsReadonly }
GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
                    {
                        credential = GoogleCredential.FromStream(stream)
                             .CreateScoped(scopes);
                    }
  credential.User = "[email protected]";
 // Create the  Calendar service.
 return new CalendarService(new BaseClientService.Initializer()
         {
         HttpClientInitializer = credential,
         ApplicationName = "Calendar Service account Authentication Sample",
         }
  );

I dont see anything major wrong with your code so i suspect the issue is that you do not have the correct type of credential file.