使用Google API 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文档或示例。 有关如何使其工作的任何建议?

谢谢

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

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

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

Google API Ruby客户端包含管理API身份validation所需的所有内容,然后请求授权的子服务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| 

你得到了这个错误,因为你正在使用传递错误的对象
ContactList::GoogleContactsApi.new(client, auth) 。 该要点希望client成为死Hurley HTTP客户端的实例,并且auth将成为来自Google Signet库的OAuth2实例。 相反,你正在尝试使用Intridea的OAuth2库 。

由于Hurley是一个死的项目,并且该要点缺乏任何unit testing,我建议您使用经过测试和工作的实现,例如与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 

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

 get() 

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