【问题标题】:With Google API Client, how to create client使用 Google API Client,如何创建客户端
【发布时间】:2017-07-13 11:28:55
【问题描述】:

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

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

我正在尝试像这样使用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 methodget' 是错误的 # 你的意思是?宝石`

我认为问题在于我没有正确发送client,并且我找不到任何说明如何创建client 的文档或示例。我怎样才能让它工作?

【问题讨论】:

  • 您是否在这里查看了 alvin 的答案:stackoverflow.com/questions/25637485/…
  • 您使用的是哪个 Signet::OAuth2::Client?
  • @jpgeek 我安装了以下内容:gem 'signet', '0.7.3'
  • 您遇到的部分问题是您正在混合 api。 ruby 客户端旨在与发现 API 一起工作。虽然联系人 API 是旧的 GData api。 github.com/tokumine/GData Gdata 谈 xml 发现谈 Json。两人说的不是同一种语言。据我所知,您是否查看过 People API?数据相似。

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


【解决方案1】:

注意:因为获取联系人列表通常需要用户的身份验证才能 读取私有数据,在下面的示例中,我假设您已经 实现了具有足够范围的 Oauth2 身份验证,您得到了 第一步的有效“令牌”。

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

gem 'google-api-client' 仍处于 alpha 阶段,并且进展非常迅速,经过大量的努力,我已经能够处理对 Youtube、Gmail 和 Analytics APIS 的经过身份验证的调用。我希望联系人 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|

【讨论】:

  • 这对我来说也是一个非常令人沮丧的问题。写了一篇与您得出类似结论的博客文章:medium.com/@_benrudolph/…
  • 你总是跑refresh! 吗?如果access_token 没有过期,感觉没必要再去谷歌了。
  • @FellowStranger 并没有完全以这种方式实现,但是通过在请求刷新之前先测试实际令牌的到期日期来保存请求似乎没问题。
【解决方案2】:

您收到该错误是因为您将错误的对象传递给
ContactList::GoogleContactsApi.new(client, auth)。该要点期望client 是死Hurley HTTP client 的一个实例,而authGoogle'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

【讨论】:

【解决方案3】:

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

get()

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

【讨论】:

  • 谢谢,但你能举个例子来说明如何做到这一点吗?
猜你喜欢
  • 2016-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多