在事件创建时检索事件ID

时间:2017-04-21 08:00:04

标签: .net google-calendar-api event-id

我有一个小应用程序来管理我的工作名单,并添加了上传到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?

由于

尼克

1 个答案:

答案 0 :(得分:0)

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