Imad
Imad

Reputation: 7490

Error while adding event to google calendar - Insufficient Permission

I am trying to add event to google calendar with .Net Console App as a client. I am getting following error.

Google.Apis.Requests.RequestError Insufficient Permission [403] Errors [ Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global] ]

on line

    service.Events.Insert(newEvent, "primary").Execute()

Here is my code

    Dim Scopes As String() = {CalendarService.Scope.CalendarReadonly}
    Dim ApplicationName As String = "Google Calendar API .NET Quickstart"
    Dim credential As UserCredential
    Using stream = New FileStream("client_secret.json", FileMode.Open, FileAccess.Read)
        Dim clientSecrets = GoogleClientSecrets.Load(stream).Secrets
        Dim credPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
        credPath = Path.Combine(credPath, "D:/calendar-dotnet-quickstart.json")
        Dim dataStore = New FileDataStore(credPath, True)
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets, Scopes, "admin", CancellationToken.None, dataStore).Result
        Console.WriteLine("Credential file saved to: " & credPath)
    End Using

    Dim service = New CalendarService(New BaseClientService.Initializer() With {.HttpClientInitializer = credential, .ApplicationName = ApplicationName})

    Dim newEvent As New Google.Apis.Calendar.v3.Data.Event()
    Dim startDate As New EventDateTime
    Dim endtDate As New EventDateTime
    Dim scope =
    startDate.DateTime = DateTime.Now.AddHours(2)
    endtDate.DateTime = DateTime.Now.AddHours(5)

    newEvent.Summary = "XYZ reminder"
    newEvent.Description = "Please contact [email protected]"
    newEvent.Start = startDate
    newEvent.End = endtDate
    newEvent.Id = "1122"


    Try
        service.Events.Insert(newEvent, "primary").Execute() 'Error here
    Catch ex As Exception
        Console.WriteLine(ex.Message & Environment.NewLine & ex.StackTrace)
    End Try

However I am able to read events that are manually created using

Dim request As EventsResource.ListRequest = service.Events.List("primary")

Also tried

  1. setting "user" in AuthorizeAsync.
  2. setting email address as a calendarId in service.Events.Insert
  3. setting scope as Dim Scopes As String() = {CalendarService.Scope.Calendar}

Upvotes: 0

Views: 1491

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17613

The problem is your scope.

Dim Scopes As String() = {CalendarService.Scope.CalendarReadonly}

You are allowed to read because your scope is CalendarReadonly.

However, to perform write operations like insert events, you need to use the read/write:

Authorization

This request requires authorization with the following scope (read more about authentication and authorization).

Scope

https://www.googleapis.com/auth/calendar

If it's not working when you changed the scope, keep in mind what the .NET Quickstart comment said:

// If modifying these scopes, delete your previously saved credentials // at ~/.credentials/calendar-dotnet-quickstart.json

You modified the scope, you need to delete your previously saved credentials for the new scope to take effect.

Upvotes: 1

Related Questions