借助Google API Client,如何创建客户端 [英] With Google API Client, how to create client

查看:134
本文介绍了借助Google API Client,如何创建客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



具体而言,我想通过Google API客户端使用以下 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。首先
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

这是与发生错误未定义的方法 get'for#你的意思是? gem`



我相信问题是我没有正确发送客户端,并且我一直找不到任何文档或者显示如何创建 client 的例子。关于如何让这个工作的任何建议?

谢谢

解决方案


注意:获取联系人列表通常需要用户认证
读取私人数据,在下面的示例中,我假设您已经使用足够的范围实现了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'


#类文件
要求'google / api_client / client_secrets.rb'#管理全局谷歌身份验证
要求'google / apis / youtube_v3'#被替换为正确的联系人API

access = {...}#您的Oauth策略返回的#Credentials对象(gem'omniauth-google-oauth2'的工作方式与魅力类似)
secrets = Google :: APIClient :: ClientSecrets.new({
web=> {access_token=> access ['token'],
refresh_token=>访问['refresh_token'],
client_id=>xxxxxxxx.apps.googleusercontent.com,
client_secret=>xxxxxxxxxxxx}})

client = Google :: Apis :: YoutubeV3 :: YouTubeService.new#根据需求行,使用服务API更新它
client.authorization = secrets.to_authorization
client.authorization.refresh!

迄今客户变量是授权的准备好查询我使用这样的对象,例如为了搜索Youtube内容

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


I'm working to use the Google API Client: https://github.com/google/google-api-ruby-client

Specifically, I want to access Google Contacts via the Google API client using the following google_contacts_api.rb: https://gist.github.com/lightman76/2357338dcca65fd390e2

I am trying to use google_contacts_api.rb like so (x is intentional and is actually the correct keys):

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

This is erring with undefined methodget' for # Did you mean? gem`

I believe the issue is I am not sending client correctly and I have been unable to find any doc or examples showing how to create client. Any suggestions on how I can get this to work?

Thanks

解决方案

Note : As getting a contacts list usually requires a user's authentication to read private data, in the example below I assume that you've already implemented Oauth2 authentication with a sufficient scope and you got a valid ’token’ from that first step.

Lot of outdated/confusing documentation online because APIs' authentication and APIs themselves have been upgraded many times. To me most useful documentation is the one at http://www.rubydoc.info/github/google/google-api-ruby-client/

gem 'google-api-client' is still in alpha and moving quite fast, after lot of struggling I've been able to work with authenticated calls to Youtube, Gmail and Analytics APIS. I hope Contacts API work the same.

The Google API Ruby Client contains everything needed to manage API authentication and then request authorized subservices APIs. No need to struggle with Hurley, Signet or other HTTP/Rest clients.

#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!

So far client variable is an authorized and ready to query object that I use like this for example in order to search for Youtube contents

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

这篇关于借助Google API Client,如何创建客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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