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

AnA*_*ice 13 ruby ruby-on-rails google-api google-contacts-api google-api-ruby-client

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

具体来说,我想使用以下内容通过Google API客户端访问Google通讯录google_contacts_api.rb: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
Run Code Online (Sandbox Code Playgroud)

这是错误的,undefined method因为#是你的意思吗?gem`

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

谢谢

Rom*_*oms 10

注意:由于获取联系人列表通常需要用户的身份验证来读取私有数据,因此在下面的示例中,我假设您已经实现了具有足够范围的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!
Run Code Online (Sandbox Code Playgroud)

到目前为止client变量是一个授权和准备查询对象,我使用这样的例子,例如为了搜索Youtube内容

client.list_searches('snippet', q: 'xxxxxx', type: 'channel'){ |res, err|
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说也是一个非常令人沮丧的问题.写了一篇与你有类似结论的博客文章:https://medium.com/@_benrudolph/end-to-end-devise-omniauth-google-api-rails-7f432b38ed75 (2认同)