Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.People.Contacts.v1
Imports Google.Apis.People.Contacts.v1.Data
Imports Google.Apis.Services
Imports Google.Apis.Util.Store
Imports System.IO
Imports System.Threading
Public Class GoogleContactsManager
Private Credential As IUserCredential
Private ReadOnly ClientSecretFileName As String = "YOUR_CLIENT_SECRET_FILE.json" ' Rename this to your downloaded JSON file
Private ReadOnly DataStoreFolderPath As String = "TokenStore" ' Folder to store user credentials
Public Async Function AuthenticateUser() As Task(Of Boolean)
Using stream As New FileStream(ClientSecretFileName, FileMode.Open, FileAccess.Read)
Credential = Await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
New String() {PeopleServiceService.Scope.ContactsReadonly}, ' Scope for read-only access to contacts
"user", ' User identifier
CancellationToken.None,
New FileDataStore(DataStoreFolderPath, True))
End Using
Return Credential IsNot Nothing
End Function
Public Async Function GetContacts() As Task(Of List(Of Person))
If Credential Is Nothing Then
Throw New InvalidOperationException("User not authenticated.")
End If
Dim peopleService = New PeopleServiceService(New BaseClientService.Initializer() With {
.HttpClientInitializer = Credential,
.ApplicationName = "Your App Name" ' Replace with your application name
})
Dim contactsList As New List(Of Person)
Dim request = peopleService.People.Connections.List("people/me")
request.PersonFields = "names,emailAddresses,phoneNumbers" ' Specify the fields you want to retrieve
request.PageSize = 100 ' Adjust page size as needed
Dim response = Await request.ExecuteAsync()
While response.Connections IsNot Nothing
contactsList.AddRange(response.Connections)
If response.NextPageToken IsNot Nothing Then
request.PageToken = response.NextPageToken
response = Await request.ExecuteAsync()
Else
Exit While
End If
End While
Return contactsList
End Function
End Class