.netgoogle-calendar-apievent-id

Retrieving Event ID at event creation time


I have a small app to manage my work roster, and am adding the ability to upload to Google Calendar.
Ideally, I would like Google to manage the allocation of an ID to each event, and it would have been nice if ID was the return value of the function that inserted a new event, but it seems that's not the way it works. How should I go about retrieving the Event ID when I create a new event. All I can think of is retrieving a list of events and searching for the one I just created, which seems like a complete kluge. And might not get the right one anyway (if for example due to a bug I create 2 duplicate events).

    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)

Is there a better way? Or should I just stop worrying and provide my own Event IDs?

Thanks

Nick


Solution

  • Posting this as answer to close the question. If you create an event using Events.insert, it will return a response which contains the eventID as the docs says: If successful, this method returns an Events resource in the response body which contains the eventID.