问题描述
我正在使用Google API客户端:
具体而言,我想通过Google API客户端使用以下 google_contacts_api.rb
:
我正在尝试使用 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
的例子。关于如何让这个工作的任何建议?
谢谢
由于API的身份验证和API,很多过期/令人困惑的文档在线他们自己已经升级了很多次。
对我来说,最有用的文档是
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 method
get' 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
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,如何创建客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!