使用Google API Client,如何创建客户端

时间:2017-02-23 05:47:24

标签: ruby-on-rails ruby google-api google-contacts google-api-ruby-client

我正在努力使用Google API客户端:https://github.com/google/google-api-ruby-client

具体来说,我想使用以下google_contacts_api.rb通过Google API客户端访问Google通讯录:https://gist.github.com/lightman76/2357338dcca65fd390e2

我正在尝试使用google_contacts_api.rb(x是故意的,实际上是正确的密钥):

require './lib/google_contacts_api.rb'
auth = User.first.authentications.first
client = OAuth2::Client.new('x', 'x', :site => 'https://accounts.google.com')
oauth2_object = OAuth2::AccessToken.new(client, auth.token)
x = ContactList::GoogleContactsApi.new(client, oauth2_object).all_contacts

undefined method获取'这是错误的。为#你的意思? gem`

我认为问题是我没有正确发送client,而且我无法找到任何显示如何创建client的文档或示例。有关如何使其工作的任何建议?

由于

3 个答案:

答案 0 :(得分:9)

  

注意:由于获取联系人列表通常需要用户进行身份验证   读取私人数据,在下面的例子中我假设你已经   实现了具有足够范围的Oauth2身份验证   第一步中有效的“令牌”。

许多过时/混乱的文档在线,因为API的身份验证和API本身已经多次升级。 对我来说,最有用的文档是http://www.rubydoc.info/github/google/google-api-ruby-client/

gem'google-api-client'仍然处于alpha状态并且移动速度非常快,经过多次努力,我已经能够使用经过身份验证的Youtube,Gmail和Analytics APIS调用。我希望Contacts API的工作原理相同。

Google API Ruby客户端包含管理API身份验证所需的所有内容,然后请求授权的子服务API。 无需与Hurley,Signet或其他HTTP / Rest客户进行斗争。

#Gemfile
gem 'google-api-client'


#Class file
require 'google/api_client/client_secrets.rb' #Manage global google authentication
require 'google/apis/youtube_v3' #To be replaced with the proper Contact API

access = {...} #Credentials object returned by your Oauth strategy (gem 'omniauth-google-oauth2' works like a charm) 
secrets = Google::APIClient::ClientSecrets.new({
"web" => {"access_token" => access['token'], 
"refresh_token" => access['refresh_token'],
"client_id" => "xxxxxxxx.apps.googleusercontent.com", 
"client_secret" => "xxxxxxxxxxxx"}})

client = Google::Apis::YoutubeV3::YouTubeService.new #As per the require line, update it with you service API
client.authorization = secrets.to_authorization
client.authorization.refresh!

到目前为止,client变量是一个经过授权且可以查询的对象,我使用这样的对象,例如为了搜索Youtube内容

client.list_searches('snippet', q: 'xxxxxx', type: 'channel'){ |res, err|

答案 1 :(得分:3)

您之所以收到该错误,是因为您正在使用将错误的对象传递给
ContactList::GoogleContactsApi.new(client, auth)。该要点希望client成为死亡Hurley HTTP clientauth的实例,成为来自Google's Signet library的OAuth2实例。相反,您正在尝试Intridea's OAuth2 libary

由于Hurley是一个死的项目,并且该要点缺乏任何单元测试,我建议您使用经过测试和工作的实现,例如与Intridea的OAuth2库兼容的google_contacts_api gem

require 'google_contacts_api'

auth = User.first.authentications.first
client = OAuth2::Client.new('x', 'x', :site => 'https://accounts.google.com')
oauth2_object = OAuth2::AccessToken.new(client, auth.token)
google_contacts_user = GoogleContactsApi::User.new(oauth2_object)

刷新令牌的问题确实属于一个单独的问题。简而言之,您必须使用Google提供的刷新令牌定期请求新令牌:

data = {
  :client_id => GOOGLE_APP_KEY,
  :client_secret => GOOGLE_APP_SECRET,
  :refresh_token => oauth2_refresh_token_for_user,
  :grant_type => "refresh_token"
}
response = ActiveSupport::JSON.decode(RestClient.post("https://accounts.google.com/o/oauth2/token"), data)
if response["access_token"].present?
  puts response["access_token"]
else
  # No Token
end
rescue RestClient::BadRequest => e
  # Bad request
rescue
  # Something else bad happened
end

答案 2 :(得分:2)

https://gist.github.com/lightman76/2357338dcca65fd390e2表示客户应该是Hurley客户。看起来你正在传递一个OAuth2 :: Client,它显然没有响应

get()

对于客户端和身份验证,我建议您尝试使用Hurley客户端和Signet :: OAuth2 :: Client身份验证。