向Google日历添加事件时出错 - 权限不足

时间:2018-03-07 06:58:56

标签: asp.net .net google-calendar-api

我正在尝试使用.Net控制台应用作为客户端将事件添加到Google日历。我收到了以下错误。

  

Google.Apis.Requests.RequestError权限不足[403]   错误[消息[权限不足]位置[ - ]   原因[insufficientPermissions] Domain [global]]

在线

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

这是我的代码

    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

但是我能够读取使用

手动创建的事件
Dim request As EventsResource.ListRequest = service.Events.List("primary")

也尝试了

  1. 设置"用户"在AuthorizeAsync
  2. 将电子邮件地址设置为service.Events.Insert
  3. 中的calendarId
  4. 将范围设置为Dim Scopes As String() = {CalendarService.Scope.Calendar}

1 个答案:

答案 0 :(得分:1)

问题在于你的范围。

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

您可以阅读,因为您的范围是CalendarReadonly。

但是,要执行诸如插入事件之类的写操作,您需要使用读/写:

Authorization

此请求需要具有以下范围的授权(请阅读有关身份验证和授权的详细信息)。

<强>范围

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

如果在更改范围时它无法正常工作,请记住.NET Quickstart评论所说的内容:

  

//如果要修改这些范围,请删除之前保存的内容   证书           //在〜/ .credentials / calendar-dotnet-quickstart.json

您修改了范围,您需要删除以前保存的凭据才能使新范围生效。