Error while adding event to google calendar - Insufficient Permission

℡╲_俬逩灬. 提交于 2019-12-24 22:32:05

问题


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 dcsdc@cdscs.ss"
    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}

回答1:


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.



来源:https://stackoverflow.com/questions/49145935/error-while-adding-event-to-google-calendar-insufficient-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!