在Ruby的访问谷歌联系人API [英] Access Google Contacts API on Ruby

查看:169
本文介绍了在Ruby的访问谷歌联系人API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我竭力要访问谷歌联系人API。

I'm struggling to access the Google Contacts API.

首先,我试过谷歌API的红宝石客户端宝石但事实证明,它的不支持联系人API

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

接下来的镜头是 google_contacts_api 宝石但我很难得到一个 oauth_access_token_for_user 的oauth2 宝石。继的oauth2说明我不知道要放什么东西在 authorization_ code_value 和基本some_password

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.

我试过如下:

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

我AP preciate如果有人可以给我一步的指示,如何访问API的详细步骤。

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

推荐答案

请确保您的应用程序设置正确,并且已启用的的谷歌开发者控制台。那就试试这个:

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)

在浏览器中

访问网​​址并登录到谷歌。你将被重定向到后来的网址将包含在参数 code 标记。它看起来像这样(这下一行是不是你运行code):

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}"

从重定向URL解析code,然后

Parse the code from the redirect url, then

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

修改

有人在评论中问道,如何将令牌传递给google_contacts_api库。 (我写的库,所以我应该知道的!)

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

标记在这个例子中的的OAuth2 ::的accessToken 对象。所有你所要做的就是将它传​​递给构造函数:

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.

这篇关于在Ruby的访问谷歌联系人API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆