Google API - I keep getting an error trying to access Calendar list

Ellen Heijmans

New member
Joined
Feb 1, 2024
Messages
2
Programming Experience
10+
I'm trying to create code in C# for accessing Google Calendar.

In console.cloud.google.com I've enabled the API, added users en added the scopes shown in the image below.

In my web application I've run Install-Package Google.Apis.Calendar.v3.

In code, the following scopes are added
C#:
String[] SCOPES = new String[] {
    "https://www.googleapis.com/auth/userinfo.profile",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/calendar",
    "https://www.googleapis.com/auth/calendar.events",
};
I've code to create the credentials.
C#:
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(ASPxUploadControl1.UploadedFiles[0].FileContent).Secrets,
    SCOPES,
    UserId,
    CancellationToken.None,
    new FileDataStore(fileName, true)).Result;

return credential;
So far it works. I can an access token back and a refresh token. But if I try to do this
C#:
CalendarList calendarList = this.service.CalendarList.List().Execute();
I get the following error:
Code:
 Google.GoogleApiException: 'The service calendar has thrown an exception. HttpStatusCode is Forbidden. Request had insufficient authentication scopes.'


Error    {Google.Apis.Requests.RequestError
Request had insufficient authentication scopes. [403]
Errors [
    Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
]
}    Google.Apis.Requests.RequestError
Can anyone help me with this?

Scopes.png
 
Last edited by a moderator:
Looks like an issue with the API itself, rather than with C#. Can you provide a link to the Google documentation that lists what scopes are needed to enumerate the calendar events?
 
Basically I was looking for a link like this:

Anyway, it looks like you have the correct scope requested. The question is, whether it was actually granted. Did you try inspecting the returned UserCredential to see what was actually given? (Yes, I saw the screenshot above where it looks like you have the restricted scopes.)

(Sorry, I've never used the Google APIs, but from what I recall from other APIs I've used, some of them let you ask for a whole set of permissions and the authentication/authorization layer responds back with what the logon session is actually granting.)
 

Latest posts

Back
Top Bottom