I keep getting an error trying to access Calendar list

Discussion in 'Google API' started by Ellen Heijmans, Feb 1, 2024.

  1. #1
    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

    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.

    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 get an access token back and a refresh token. But if I try to do this

    CalendarList calendarList = this.service.CalendarList.List().Execute();

    I get the following error: 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?

    upload_2024-2-1_13-34-29.png
     
    Ellen Heijmans, Feb 1, 2024 IP
  2. GreenHost.Cloud

    GreenHost.Cloud Active Member

    Messages:
    450
    Likes Received:
    33
    Best Answers:
    3
    Trophy Points:
    73
    #2
    It looks like the error is indicating that the authenticated user does not have the necessary permissions to access the calendar resource.
    You can try to update the scopes in your code to include the required permissions. In this case, you may need to add the `https://www.googleapis.com/auth/calendar.readonly` scope to allow read-only access to the user's calendars.
    You can update your `SCOPES` array to include the additional scope:

    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",
        "https://www.googleapis.com/auth/calendar.readonly",
    };
    Code (markup):
    After updating the scopes, you will need to re-authorize the user with the updated permissions to generate a new access token with the correct scopes.
    Once you have updated the scopes and re-authorized the user, try to execute the `CalendarList.List()` method again to see if the error has been resolved.
     
    GreenHost.Cloud, Jun 6, 2024 IP