googleapis/google-api-ruby-client

How to use OAuth token with People API?

dacur opened this issue · 1 comments

dacur commented

We are migrating a large enterprise application from the Google Contacts API to the People API.

Before, using the OAuth token to make requests to the Contacts API was easy. We would authenticate with OAuth, and then pass the token to the Google Contacts API like so:

# access_token is the token we receive from OAuth
@user = GoogleContactsApi::User.new(access_token)

# make a request
contact_objects = user.contacts

The PeopleService code shows how to use an API key, and then only makes mention of the OAuth token:

        #  API key. Your API key identifies your project and provides you with API access,
        #  quota, and reports. Required unless you provide an OAuth 2.0 token

But I've been unable to find an example of how to use the OAuth token to make requests to the People API using the Ruby gem.

Could you please provide a simple example of how to make a People API request using the OAuth token? Specifically, we want access to a user's contacts' email address and phone numbers. I believe we will be using get_people. If you could provide this specific example, that would be wonderful.

Thank you! 😄

dacur commented

I think this question can be closed.

It looks like we needed to access access_token.token and set it like so:

require 'google/apis/people_v1'

class GooglePeopleApiWrapper
  attr_reader :service, :access_token

  def initialize(oauth_object)
    # outh_object is the `access_token` from my question, which is 
    # provided in the OAuth response
    @oauth_object = oauth_object 
    @service = Google::Apis::PeopleV1::PeopleServiceService.new
    @service.authorization = @oauth_object.token
  end

  def fetch_contacts
    # Fetch the next 10 events for the user
    contact_objects = @service.list_person_connections(
      'people/me',
      page_size: 10,
      person_fields: 'names,emailAddresses,phoneNumbers',
    )
    
    etc...
  end
end

I hope this example helps someone else. Thanks.