0

我有一个小应用程序来管理我的工作名册,并且正在添加上传到 Google 日历的功能。
理想情况下,我希望 Google 管理每个事件的 ID 分配,如果 ID 是插入新事件的函数的返回值,那就太好了,但它似乎不是这样工作的。创建新事件时,我应该如何检索事件 ID。我能想到的只是检索事件列表并搜索我刚刚创建的事件,这似乎是一个完整的 kluge。并且可能无论如何都找不到正确的(例如,如果由于错误我创建了 2 个重复事件)。

    Dim MyCredential As UserCredential
    Dim stream As New FileStream("client_secret.json", FileMode.Open, FileAccess.Read)
    Dim credPath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal)
    credPath = SettingsDirectory & "Tripster.json"
    MyCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
    MsgBox("Credential saved to " & credPath)

    'create google calendar API service
    Dim MyInitializer As New BaseClientService.Initializer()
    MyInitializer.HttpClientInitializer = MyCredential
    MyInitializer.ApplicationName = "Tripster"
    Dim MyService As New CalendarService(MyInitializer)

    'insert 
    Dim CalendarEvent As New Data.Event
    Dim StartDateTime As New Data.EventDateTime
    Dim A As New Date(2017, 5, 20, 12, 0, 0)
    StartDateTime.DateTime = A
    Dim b As Date
    b = A.AddHours(2)
    Dim EndDateTime As New Data.EventDateTime
    EndDateTime.DateTime = b
    CalendarEvent.Start = StartDateTime
    CalendarEvent.End = EndDateTime
    'CalendarEvent.Id = System.Guid.NewGuid.ToString
    CalendarEvent.Description = "Test"
    CalendarEvent.Summary = "A test event inserted by Tripster"

    MyService.Events.Insert(CalendarEvent, "primary").Execute()

    'define parameters of request
    Dim MyRequest As New EventsResource.ListRequest(MyService, "primary")
    MyRequest.TimeMin = A
    MyRequest.ShowDeleted = False
    MyRequest.SingleEvents = True
    MyRequest.MaxResults = 10
    MyRequest.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime

    Dim MyEvents As Events = MyRequest.Execute
    Dim OutputString As String = "Upcoming Events" & vbNewLine
    If Not MyEvents.Items Is Nothing AndAlso MyEvents.Items.Count > 0 Then
        For Each EventItem As Google.Apis.Calendar.v3.Data.Event In MyEvents.Items
            If EventItem.Description = CalendarEvent.Description Then
                Dim whenstring As String = EventItem.Start.DateTime.ToString
                If String.IsNullOrEmpty(whenstring) Then
                    whenstring = EventItem.Start.Date
                End If
                OutputString += whenstring & "  " & EventItem.Description & " " & EventItem.Summary & "  " & EventItem.Id & vbNewLine
                Exit For
            End If
        Next
    Else
        OutputString += "No upcoming events found." & vbNewLine
    End If
    MsgBox(OutputString)

有没有更好的办法?还是我应该停止担心并提供我自己的事件 ID?

谢谢

缺口

4

1 回答 1

0

将此作为答案发布以关闭问题。如果您使用Events.insert创建事件,它将返回一个包含 eventID 的响应,如文档所述:如果成功,此方法将在包含 eventID 的响应正文中返回一个 Events 资源。

于 2017-06-18T15:46:48.893 回答