Skip to content

Instantly share code, notes, and snippets.

@syafiqfaiz
Last active June 27, 2023 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syafiqfaiz/d102f56bc2a4ee7a7ef02eaa48743bd5 to your computer and use it in GitHub Desktop.
Save syafiqfaiz/d102f56bc2a4ee7a7ef02eaa48743bd5 to your computer and use it in GitHub Desktop.
How to use Google People V1 API using service acccount in Ruby.

How to use Google People V1 API using service acccount in Ruby.

  1. You need Google Workplace Account
  2. Create/add contacts to the account
  3. Create Service Account from the google workplace account cloud console.
  4. Enable the People API
  5. Add the service account to the google workplace Domain Wide Delegation
  6. Update the code to your settings.
  7. Run the code
require 'google/apis/people_v1'
require 'googleauth'
# Set up authorization
scopes = ['https://www.googleapis.com/auth/contacts']
credentials = Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open('g_suite_service_account.json'),
scope: scopes,
)
credentials.sub = 'the_account_you_want_to_use@email.com'
# Authorize the client
client = Google::Apis::PeopleV1::PeopleServiceService.new
client.authorization = credentials
response = client.list_person_connections(
'people/me',
page_size: 100,
person_fields: 'names,emailAddresses'
)
response.connections.each do |connection|
puts connection.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment