在事件创建时检索事件 ID
Retrieving Event ID at event creation time
我有一个小应用程序来管理我的工作名册,并且正在添加上传到 Google 日历的功能。
理想情况下,我希望 Google 管理每个事件的 ID 分配,如果 ID 是插入新事件的函数的 return 值就好了,但似乎不是它的工作方式。
创建新事件时,我应该如何检索事件 ID。我所能想到的就是检索事件列表并搜索我刚刚创建的事件,这似乎是一个完整的问题。并且可能无法获得正确的(例如,如果由于错误我创建了 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?
谢谢
尼克
将此作为答案发布以关闭问题。
如果您使用 Events.insert 创建一个事件,它将 return 一个包含 eventID 的响应,如文档所述:如果成功,此方法 return 是响应主体中的一个事件资源,其中包含事件ID。
我有一个小应用程序来管理我的工作名册,并且正在添加上传到 Google 日历的功能。
理想情况下,我希望 Google 管理每个事件的 ID 分配,如果 ID 是插入新事件的函数的 return 值就好了,但似乎不是它的工作方式。
创建新事件时,我应该如何检索事件 ID。我所能想到的就是检索事件列表并搜索我刚刚创建的事件,这似乎是一个完整的问题。并且可能无法获得正确的(例如,如果由于错误我创建了 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?
谢谢
尼克
将此作为答案发布以关闭问题。 如果您使用 Events.insert 创建一个事件,它将 return 一个包含 eventID 的响应,如文档所述:如果成功,此方法 return 是响应主体中的一个事件资源,其中包含事件ID。