1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load main() End Sub '' Calendar scopes which is initialized on the main method. Dim scopes As IList(Of String) = New List(Of String)() '' Calendar service. Dim service As Google.Apis.Calendar.v3.CalendarService Sub main() scopes.Add(Google.Apis.Calendar.v3.CalendarService.Scope.Calendar) service = GetCalendarService() LoadCalendars() Dim CalendarToTarget = GetCalendarIDFromSummary("MyCalendarSummary") Getevent_id(CalendarToTarget, "Summary") 'InsertCalendar(CalendarToTarget, New DateTime(2023, 1, 27, 10, 0, 0), 2, "Test") End Sub Private Function GetCalendarService() As Google.Apis.Calendar.v3.CalendarService 'Google.Apis.Auth.OAuth2.ServiceAccountCredential 'access json file and send to google Dim MyToken As New Google.Apis.Auth.OAuth2.Responses.TokenResponse ' With refreshToken = Refresh_token 'Dim credential As Google.Apis.Auth.OAuth2.UserCredential Dim credential As Google.Apis.Auth.OAuth2.ServiceAccountCredential Using stream As New IO.FileStream("client_secrets.json", IO.FileMode.Open, IO.FileAccess.Read) Dim MyInint = New Google.Apis.Auth.OAuth2.ServiceAccountCredential.Initializer("svc-texttospeech@ancient-edition-306401.iam.gserviceaccount.com").FromPrivateKey("-----BEGIN PRIVATE KEY-----MIIEvQIBADANBgkqhkiG9xxxxxxxxxx-----END PRIVATE KEY-----") MyInint.Scopes = scopes credential = New Google.Apis.Auth.OAuth2.ServiceAccountCredential(MyInint) End Using Return New Google.Apis.Calendar.v3.CalendarService(New Google.Apis.Services.BaseClientService.Initializer() With {.ApplicationName = "GoogleTextToSpeech", .HttpClientInitializer = credential}) End Function Private Function GetCalendarServiceAsUser() As Google.Apis.Calendar.v3.CalendarService Dim credential As Google.Apis.Auth.OAuth2.UserCredential Using stream = New FileStream("client_secrets.json", FileMode.Open, FileAccess.ReadWrite) Dim credPath As String = "token.json" credential = Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync(Google.Apis.Auth.OAuth2.GoogleClientSecrets.Load(stream).Secrets, scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result End Using Return New Google.Apis.Calendar.v3.CalendarService(New Google.Apis.Services.BaseClientService.Initializer() With {.ApplicationName = "GoogleTextToSpeech", .HttpClientInitializer = credential}) End Function Public Function Getevent_id(CalendarID As String, testo As String) Dim EventRequest As Google.Apis.Calendar.v3.EventsResource.ListRequest = service.Events.List(CalendarID) EventRequest.OrderBy = Google.Apis.Calendar.v3.EventsResource.ListRequest.OrderByEnum.Updated EventRequest.TimeMin = New DateTime(Date.Now.Year, 1, 1, 0, 0, 0) ' 'EventRequest.Q = testo ' For Each CalendarEvent As Google.Apis.Calendar.v3.Data.Event In EventRequest.Execute.Items 'If CalendarEvent.Start.DateTime > Date.Parse("01-01-2023") Then Debug.WriteLine(CalendarEvent.Summary & " - " & CalendarEvent.Description & " START: " & CalendarEvent.Start.DateTime & " STATUS: " & CalendarEvent.Status) ' End If Next Return 0 End Function Sub InsertCalendar(CalendarID As String, StartDate As DateTime, Duration As Double, Description As String) Dim EventRequest As Google.Apis.Calendar.v3.EventsResource.ListRequest = service.Events.List(CalendarID) Dim CalendarEvent As New Google.Apis.Calendar.v3.Data.Event Dim StartDateTime As New Google.Apis.Calendar.v3.Data.EventDateTime StartDateTime.DateTime = StartDate Dim b As Date b = StartDate.AddHours(Duration) Dim EndDateTime As New Google.Apis.Calendar.v3.Data.EventDateTime EndDateTime.DateTime = b CalendarEvent.Start = StartDateTime CalendarEvent.End = EndDateTime 'CalendarEvent.Id = System.Guid.NewGuid.ToString 'Let google generate this for us CalendarEvent.Description = Description service.Events.Insert(CalendarEvent, CalendarID).Execute() End Sub Private objCalendars As Google.Apis.Calendar.v3.Data.CalendarList '///Holds all calendars. Private Sub LoadCalendars() Dim objCalendarListRequest As Google.Apis.Calendar.v3.CalendarListResource.ListRequest = service.CalendarList.List If False Then 'This only needs to be executed once if the calendar is not accepted programmatically once already. Dim MyCalendarItem = New Google.Apis.Calendar.v3.Data.CalendarListEntry() 'Calendar Invites must be accepted by their ID before queryed. MyCalendarItem.Id = "c_be3cf2fda38d9966ahahahashadfafac8780e7ecc69c@group.calendar.google.com" Dim MyInsertRequest As Google.Apis.Calendar.v3.CalendarListResource.InsertRequest = service.CalendarList.Insert(MyCalendarItem) Debug.WriteLine(MyInsertRequest.Execute.Summary) End If objCalendars = objCalendarListRequest.Execute() If objCalendars Is Nothing Or objCalendars.Items.Count = 0 Then '** No calendars. Something is wrong. Give up and die. Debug.WriteLine("No calendars found? That's weird.") Else For Each objCal As Google.Apis.Calendar.v3.Data.CalendarListEntry In objCalendars.Items Console.WriteLine("Calendar: " & objCal.Summary) Next End If End Sub Private Function GetCalendarIDFromSummary(ByVal Summary) As String If objCalendars IsNot Nothing AndAlso objCalendars.Items.Count > 0 Then For Each objCal As Google.Apis.Calendar.v3.Data.CalendarListEntry In objCalendars.Items Debug.WriteLine(objCal.Summary & " " & objCal.Description) If objCal.Summary = Summary Then Return objCal.Id End If Next End If Return "" 'Should have found a match if objCalendars was properly loaded. End Function |
End Class