4

I'm struggling to access the Google Contacts API.

First I tried the google-api-ruby-client gem but it turned out that it does not support the Contacts API.

Next shot was the google_contacts_api gem but I struggle to get a oauth_access_token_for_user with the oAuth2 gem. When following the oAuth2 instructions I don't know what to put in authorization_code_value and Basic some_password.

I tried the following:

require 'oauth2'
client = OAuth2::Client.new(ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], :site => 'http://localhost:9292')
=> #<OAuth2::Client:0x007fcf88938758 @id="blabla.apps.googleusercontent.com", @secret="blabla", @site="http://localhost:9292", @options={:authorize_url=>"/oauth/authorize", :token_url=>"/oauth/token", :token_method=>:post, :connection_opts=>{}, :connection_build=>nil, :max_redirects=>5, :raise_errors=>true}>

client.auth_code.authorize_url(:redirect_uri => 'http://localhost:9292')
=> "http://localhost:9292/oauth/authorize?client_id=blabla.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A9292&response_type=code"

token = client.auth_code.get_token('authorization_code_value', :redirect_uri => 'http://localhost:9292', :headers => {'Authorization' => 'Basic some_password'})
=> Faraday::ConnectionFailed: Connection refused - connect(2) for "localhost" port 9292

I would appreciate if someone could give me detailed step by step instructions how to access the API.

2 Answers 2

6

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.

4
  • Thanks alot alvin, but I am getting stuck. I am able to get the access token whick is in this format: #<OAuth2::AccessToken:0x000000037a69d34>. But now how am I going to continue with the google_contacts_api gem. How should I pass the token to it. I would appreciate if you could give something a bit detailed?
    – ben
    Sep 25, 2014 at 22:55
  • All you have to do is pass the token to the constructor: GoogleContactsApi::User.new(token)
    – alvin
    Sep 26, 2014 at 8:34
  • Your answer helped me authorize and even get the token. When I pass the token to the constructor as you have suggested it is giving an error. I have posted a question about this stackoverflow.com/questions/26052480/…. Plz have a look at the question
    – ben
    Sep 26, 2014 at 8:56
  • What I want to do is, I want to access all the email addresses in my Gmail contacts. You can also suggest a way to do this. Thanks.
    – ben
    Sep 26, 2014 at 9:00
1

It looks like you're authenticating against localhost (should only be referring to localhost in the context of redirecting after authentication). You should be authenticating against Google's OAuth server somewhere in there.

See: https://github.com/google/google-api-ruby-client/blob/master/lib/google/api_client.rb#L165

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.