2017-04-21 52 views
0

我有一個小應用程序來管理我的工作名冊,並且添加了上傳到Google日曆的功能。
理想情況下,我希望Google管理每個事件的ID分配情況,如果ID是插入新事件的函數的返回值,那本來就不錯,但看起來這不是它的工作方式。 當我創建一個新事件時,我應該如何去檢索事件ID。我能想到的只是檢索一系列事件並搜索我剛剛創建的事件,這看起來像是一個完整的事情。無論如何可能無法得到正確的結果(例如,如果由於錯誤,我創建了2個重複事件)。在事件創建時檢索事件ID

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?

感謝

尼克

+0

謝謝noogui。之前我沒有注意到。似乎運作良好。如果你把這個作爲答案寫了,我會加註它。 – nicke

回答

0

發佈此作爲答案,關閉問題。 如果您使用Events.insert創建事件,它將返回一個響應,其中包含eventID,如文檔所述:如果成功,此方法將在響應正文中返回包含eventID的Events資源。