Search code examples
c#google-apigoogle-oauthgoogle-calendar-apigoogle-api-dotnet-client

insufficient authentication scopes error when trying to create new event


I want to create a event in Google Calendar with C#. I am at the first stages in programming but I would like to try to solve this problem.

the code:

private void GoogleAPI_Add_events()
    {
        UserCredential credential;

        using (var stream =
            new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
        {
            // The file token.json stores the user's access and refresh tokens, and is created
            // automatically when the authorization flow completes for the first time.
            string credPath = "token.json";
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        }

        // Create Google Calendar API service.
        var service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        // Define parameters of request.
        EventsResource.ListRequest request = service.Events.List("primary");
        request.TimeMin = DateTime.Now;
        request.ShowDeleted = false;
        request.SingleEvents = true;
        request.MaxResults = 8;
        request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
        var ev = new Event();
        EventDateTime start = new EventDateTime();
        start.DateTime = new DateTime(2021, 3, 11, 10, 0, 0);

        EventDateTime end = new EventDateTime();
        end.DateTime = new DateTime(2021, 3, 15, 10, 0, 0);
        ev.Start = start;
        ev.End = end;
        ev.Summary = "New Event";
        ev.Description = "Description...";

        var calendarId = "primary";
        Event recurringEvent = service.Events.Insert(ev, calendarId).Execute();
        MessageBox.Show("New evento creato");
    }

I get this error:

Google.GoogleApiException: 'Google.Apis.Requests.RequestError Request had insufficient authentication scopes. [403] Errors [


Solution

  • Request had insufficient authentication scopes.

    Basically means that the user that you have currently authenticated your application has not granted you the permissions you need to do what you are trying to do.

    You are attempting to use the Events.insert method which requires one of the following scopes.

    enter image description here

    You have not posted what you are sending as Scope but I can guess it's not one of those.

    Note:

    Remember when you do change scope that you either need to change the "user" text or remove the credentials file for the user stored in credPath or its not going to request new authorization for your application.

    In the code below the term "user" denotes the user who you are logging in with if you have already logged in with this user then the credeitnals for this user is being stored by FileDataStore in credsPath directory, you should have a directory called token.json.

    Either change "user" to some other string say "user1" or go in to that directory and delete the file.

     credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;