With Google API 客户端,如何创建客户端
With Google API Client, how to create 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
get' for # 的错误是什么意思? gem`
我认为问题是我没有正确发送 client
,而且我一直无法找到说明如何创建 client
的任何文档或示例。我怎样才能让它工作?
https://gist.github.com/lightman76/2357338dcca65fd390e2 说客户应该是 Hurley 客户。看起来你正在传递一个 OAuth2::Client,它显然没有响应
get()
对于客户端和身份验证,我建议尝试使用 Hurley 客户端和 Signet::OAuth2::Client 身份验证。
您收到该错误是因为您将错误的对象传递给
ContactList::GoogleContactsApi.new(client, auth)
。该要点期望 client
成为死者 Hurley HTTP client and auth
to be an OAuth2 instance from Google's Signet library. Instead you're trying to 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
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.
很多outdated/confusing在线文档,因为APIs的身份验证和APIs本身已经升级了很多次。
对我来说最有用的文档是 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|
我正在努力使用 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
get' for # 的错误是什么意思? gem`
我认为问题是我没有正确发送 client
,而且我一直无法找到说明如何创建 client
的任何文档或示例。我怎样才能让它工作?
https://gist.github.com/lightman76/2357338dcca65fd390e2 说客户应该是 Hurley 客户。看起来你正在传递一个 OAuth2::Client,它显然没有响应
get()
对于客户端和身份验证,我建议尝试使用 Hurley 客户端和 Signet::OAuth2::Client 身份验证。
您收到该错误是因为您将错误的对象传递给
ContactList::GoogleContactsApi.new(client, auth)
。该要点期望 client
成为死者 Hurley HTTP client and auth
to be an OAuth2 instance from Google's Signet library. Instead you're trying to 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
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.
很多outdated/confusing在线文档,因为APIs的身份验证和APIs本身已经升级了很多次。 对我来说最有用的文档是 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|