Last active
August 11, 2022 06:20
-
-
Save khanhkhuu/afb8cb4eb6e0318be3f6f3d927dc0554 to your computer and use it in GitHub Desktop.
Connect Google from WinForm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.IO | |
Imports System.Threading | |
Imports Google.Apis.Auth.OAuth2 | |
Imports Google.Apis.Services | |
Imports Google.Apis.Sheets.v4 | |
Imports Google.Apis.Sheets.v4.Data | |
Imports Google.Apis.Util.Store | |
Public Class Google | |
Private Scopes As String() = {SheetsService.Scope.Spreadsheets} | |
Private ApplicationName As String = "Mapmy" | |
Private Function GetService() | |
Dim credential As UserCredential | |
Using stream = New FileStream("credentials.json", FileMode.Open, FileAccess.Read) | |
Dim credPath As String = "token.json" | |
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromStream(stream).Secrets, Scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result | |
Console.WriteLine("Credential file saved to: " & credPath) | |
End Using | |
Dim initializer = New BaseClientService.Initializer() | |
Dim service = New SheetsService(New BaseClientService.Initializer() With { | |
.HttpClientInitializer = credential, | |
.ApplicationName = ApplicationName | |
}) | |
Return service | |
End Function | |
Public Function GetValues(ByVal spreadsheetId As String, ByVal range As String) | |
Dim service = GetService() | |
Dim request As SpreadsheetsResource.ValuesResource.GetRequest = service.Spreadsheets.Values.[Get](spreadsheetId, range) | |
Dim response As ValueRange = request.Execute() | |
Dim values As IList(Of IList(Of Object)) = response.Values | |
Return values | |
End Function | |
Public Function AppendRow(ByVal spreadsheetId As String, ByVal sheetName As String, newRow As List(Of Object)) | |
Dim service As SheetsService = GetService() | |
Dim body As ValueRange = New ValueRange() | |
body.Values = New List(Of IList(Of Object)) | |
body.Values.Add(newRow) | |
Dim request As SpreadsheetsResource.ValuesResource.AppendRequest = service.Spreadsheets.Values.Append( | |
body, | |
spreadsheetId, | |
sheetName | |
) | |
request.ValueInputOption = ValueInputOptionEnum.RAW | |
Dim response As AppendValuesResponse = request.Execute() | |
Return response.Updates | |
End Function | |
Public Function UpdateValue(ByVal spreadsheetId As String, ByVal range As String, newVal As Object) | |
Dim service As SheetsService = GetService() | |
Dim body As New ValueRange() | |
Dim row As New List(Of Object) From { | |
newVal | |
} | |
body.Values = New List(Of IList(Of Object)) From { | |
row | |
} | |
Dim request As SpreadsheetsResource.ValuesResource.UpdateRequest = service.Spreadsheets.Values.Update(body, spreadsheetId, range) | |
request.ValueInputOption = ValueInputOptionEnum.RAW | |
Dim response As UpdateValuesResponse = request.Execute() | |
Return response | |
End Function | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment