Access Google Contacts API on Ruby

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 04:43:28

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/25637485/access-google-contacts-api-on-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!