Access Google Contacts API on Ruby

Access Google Contacts API on Ruby

Make sure your app is set up properly and that you've enabled the Contacts API in the Google Developers Console. Then try this:

CLIENT_ID = '?????.apps.googleusercontent.com'
CLIENT_SECRET = 'your_secret'
REDIRECT_URI = 'your_redirect_uri'
client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET,
site: 'https://accounts.google.com',
token_url: '/o/oauth2/token',
authorize_url: '/o/oauth2/auth')
url = client.auth_code.authorize_url(scope: "https://www.google.com/m8/feeds",
redirect_uri: REDIRECT_URI)

Visit url in your browser and log in to Google. The url you are redirected to afterwards will contain the token in the parameter code. It will look like this (this next line is not code you run):

actual_redirect_url = "#{REDIRECT_URI}?code=#{code}"

Parse the code from the redirect url, then

token = client.auth_code.get_token(code, :redirect_uri => REDIRECT_URI)

Edit

Someone asked in the comments how to pass the token to the google_contacts_api library. (I wrote the library, so I should know!)

token is an OAuth2::AccessToken object in this example. All you have to do is pass it to the constructor:

user = GoogleContactsApi::User.new(token)

To be extra clear, the constructor accepts the token object, not a string.

How to access Google Contacts API in ruby

Edited to add info about the Enumerable implementation

(I wrote the gem.)

There was a bug in the documentation. groups and contacts are instances of classes that implement Enumerable, which doesn't provide the [] method, but does provide the first method.

So, try groups.first instead of groups[0]. Likewise, use contacts.first instead of contacts[0]. My bad! (I probably did a to_a in my head.)


Response to Update

To answer the second half of the question, it looks like you found the relevant convenience methods for Contact and Group, in particular the Contact.primary_email method. See more methods in the (somewhat incomplete, sorry) YARD docs.

To get all the emails, you basically need to iterate over the returned contacts. As I mentioned in the updated response to the first part of your question, groups and contacts have all the methods of Enumerable. (Enumerable documentation). Here are some examples:

# What are all the groups called?
user.groups.map(&:title)

# Find group by title. (Returns nil if no such group.)
group = user.groups.select { |g| g.title = "Group Name" }

# Get all primary emails from a group
group.contacts.map(&:primary_email)

# Get all primary emails from all contacts regardless of group
user.contacts.map(&:primary_email)

You only need to use the Hashie::Mash methods to access data when no convenience accessor is provided (for example, if Google starts returning extra data the gem hasn't accounted for yet). The use case you described doesn't require this.

P.S. In the future, you might want to open a new question instead of editing your existing question.

Create Google Contact through API in Ruby On Rails

The Google People API is read-only and can't be used to update or add new contacts. For that, you need to use the Google Contacts API which is, unfortunately, not supported by the google-api-client gem.

To access the Google Client API, you can try using this gem:

google_contacts_api

If you run into trouble setting it up, check out this stackoverflow question:

access google contacts api

It has a helpful answer written by the same person who wrote the gem.

EDIT

It looks like development on the google_contacts_api gem stopped before functionality for creating contacts was added. I'm looking through the various gems on Github and they're all in states of disrepair / death.

As much as it pains me to say it, your best bet might be to access Google Contacts directly via their web API.

Best of luck and sorry for the disheartening answer.

When attempting to retrieve Google Contacts via PeopleV1 API in Ruby Rails, why do I get ArgumentError: unknown keyword: person_fields?

The correct attribute is fields not person_fields. See docs here.

rails google-api-client contact api

The Google Contacts API is on Google's older GData API standard and is not supported by the discovery API. There is a pretty extensive guide for plain Ruby and a helper gem. Retrieving all contacts doesn't provide a Ruby sample but the Python sample should translate pretty easily.

def PrintAllContacts(gd_client):
feed = gd_client.GetContacts()
for i, entry in enumerate(feed.entry):
print '\n%s %s' % (i+1, entry.name.full_name.text)
if entry.content:
print ' %s' % (entry.content.text)
# Display the primary email address for the contact.
for email in entry.email:
if email.primary and email.primary == 'true':
print ' %s' % (email.address)


Related Topics



Leave a reply



Submit