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

     2023-04-15     114

关键词:

【中文标题】使用 Google API Client,如何创建客户端【英文标题】:With Google API Client, how to create client 【发布时间】:2017-07-13 11:28:55 【问题描述】:

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

具体来说,我想通过 Google API 客户端使用以下google_contacts_api.rb:https://gist.github.com/lightman76/2357338dcca65fd390e2 访问 Google 通讯录

我正在尝试像这样使用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 methodget' 是错误的 # 你的意思是?宝石`

我认为问题在于我没有正确发送client,并且我找不到任何说明如何创建client 的文档或示例。我怎样才能让它工作?

【问题讨论】:

您是否在这里查看了 alvin 的答案:***.com/questions/25637485/…? 您使用的是哪个 Signet::OAuth2::Client? @jpgeek 我安装了以下内容:gem 'signet', '0.7.3' 您遇到的部分问题是您正在混合 api。 ruby 客户端旨在与发现 API 一起工作。虽然联系人 API 是旧的 GData api。 github.com/tokumine/GData Gdata 谈 xml 发现谈 Json。两人说的不是同一种语言。据我所知,您是否查看过 People API?数据相似。 【参考方案1】:

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

许多过时/令人困惑的在线文档,因为 API 的身份验证和 API 本身已经升级了很多次。 对我来说最有用的文档是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|

【讨论】:

这对我来说也是一个非常令人沮丧的问题。写了一篇与您得出类似结论的博客文章:medium.com/@_benrudolph/… 你总是跑refresh! 吗?如果access_token 没有过期,感觉没必要再去谷歌了。 @FellowStranger 并没有完全以这种方式实现,但是通过在请求刷新之前先测试实际令牌的到期日期来保存请求似乎没问题。【参考方案2】:

您收到该错误是因为您将错误的对象传递给ContactList::GoogleContactsApi.new(client, auth)。该要点期望client 是死Hurley HTTP client 的一个实例,而auth 是Google's Signet library 的一个OAuth2 实例。相反,您正在尝试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

【讨论】:

谢谢,但是这个例子不涉及刷新令牌的需要,这是我卡住的地方。知道如何刷新此推荐 gem 的令牌吗? 您必须自己刷新令牌。有关示例,请参见 ***.com/questions/21707734/… 和 github.com/zquestz/omniauth-google-oauth2/issues/37。我在上面包含了一个简短的。【参考方案3】:

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

get()

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

【讨论】:

谢谢,但你能举个例子来说明如何做到这一点吗?

如何使用 google-api-ruby-client People API 传递 OAuth 令牌?

】如何使用google-api-ruby-clientPeopleAPI传递OAuth令牌?【英文标题】:HowtopassOAuthtokenusinggoogle-api-ruby-clientPeopleAPI?【发布时间】:2021-12-1321:12:18【问题描述】:我们正在将一个大型企业应用程序从GoogleContactsAPI迁移到PeopleAPI。以前,... 查看详情

通过 googleapis/google-api-php-client 创建新的 offerClass 时返回“缺少资源”错误

】通过googleapis/google-api-php-client创建新的offerClass时返回“缺少资源”错误【英文标题】:"Missingresource"errorreturnedwhencreatingnewofferClassviagoogleapis/google-api-php-client【发布时间】:2021-09-1816:46:10【问题描述】:更新:我现在刚... 查看详情

Flutter - 如何使用 Google Drive API 下载公共文件

】Flutter-如何使用GoogleDriveAPI下载公共文件【英文标题】:Flutter-HowtodownloadpublicfilesusingGoogleDriveAPI【发布时间】:2020-12-1413:35:11【问题描述】:我第一次尝试使用GoogleAPI、GoogleDriveAPI,我正在使用GoogleSignIn对用户进行身份验证。我... 查看详情

使用 google-api-python-client 时出错

】使用google-api-python-client时出错【英文标题】:Errorwhileusinggoogle-api-python-client【发布时间】:2017-09-0401:14:05【问题描述】:我正在使用Anaconda,使用google-api-python-client时出现错误。错误:fromapiclient.discoveryimportbuildImportError:Nomodulen... 查看详情

通过 OAuth 2.0 自动使用 google-api-dotnet-client

】通过OAuth2.0自动使用google-api-dotnet-client【英文标题】:Automateduseofgoogle-api-dotnet-clientwithOAuth2.0【发布时间】:2012-09-0721:45:07【问题描述】:我有一堆密钥:客户端ID、客户端密码、API密钥但所有文档都显示使用人工交互进行身份... 查看详情

如何将 google-api-client 用于 Google Cloud Logging

】如何将google-api-client用于GoogleCloudLogging【英文标题】:Howtousegoogle-api-clientforGoogleCloudLogging【发布时间】:2021-05-2723:49:09【问题描述】:我想通过python脚本访问GoogleCloudPlatformLogging。我可以从https://cloud.google.com/logging/docs/reference/v 查看详情

通过 google-api-php-client 来自 Youtube API 的 accessNotConfigured 错误 (403)

...:2019-08-3104:13:21【问题描述】:我已完成所有步骤,可以使用api控制台创建事件和编辑事件,但我无法使用Google的APIPHP库以编 查看详情

使用服务帐户 oauth2client.client.CryptoUnavailableError 的 Google API 访问:没有可用的加密库

】使用服务帐户oauth2client.client.CryptoUnavailableError的GoogleAPI访问:没有可用的加密库【英文标题】:GoogleAPIaccessusingServiceAccountoauth2client.client.CryptoUnavailableError:Nocryptolibraryavailable【发布时间】:2015-02-0300:45:30【问题描述】:我正在... 查看详情

如何使用可可豆荚安装 Google Api

】如何使用可可豆荚安装GoogleApi【英文标题】:HowtoinstallGoogleApiwithcocoapod【发布时间】:2015-12-0218:29:09【问题描述】:我无法使用可可豆荚安装googleapi这是我的豆荚:platform:ios,\'8.0\'use_frameworks!target\'testGoogleFacebook\'dopod\'Google-API-... 查看详情

如何使用 Google Apps 脚本执行 API + Java 创建 Google 表单?

】如何使用GoogleApps脚本执行API+Java创建Google表单?【英文标题】:HowtocreateaGoogleFormusingGoogleAppsScriptExecutionAPI+Java?【发布时间】:2016-04-2920:10:52【问题描述】:我尝试了一个简单的Java命令行应用程序,它向GoogleAppsScriptExecutionAPI发... 查看详情

使用 gmail-api 和 google-api-php-client 发送电子邮件

】使用gmail-api和google-api-php-client发送电子邮件【英文标题】:sendemailusinggmail-apiandgoogle-api-php-client【发布时间】:2014-09-1610:30:26【问题描述】:我正在使用https://github.com/google/google-api-php-client,我想使用用户的授权gmail帐户发送测... 查看详情

空批次在使用 google-api-php-client 的 DoubleClick Search API 中遇到错误

】空批次在使用google-api-php-client的DoubleClickSearchAPI中遇到错误【英文标题】:EmptybatchencounterederrorinDoubleClickSearchAPIusinggoogle-api-php-client【发布时间】:2017-10-0317:34:38【问题描述】:在使用google-api-php-client库将离线转化上传到DoubleCl... 查看详情

Pyinstaller 不包括 google-api-python-client

...述】:我已经编写了一个Kivy应用程序,现在我正在尝试使用PyInstaller为MacOS打包它。它使用GoogleCalendarAPI,所以我需要使用google-api-python-cl 查看详情

如何使用 Google API 进行离线访问

】如何使用GoogleAPI进行离线访问【英文标题】:Howtogetoffline-accesswithGoogleAPIs【发布时间】:2017-09-1915:39:15【问题描述】:我正在尝试使用GoogleAPI获取offlineaccess。我必须从Google日历中提取数据,而无需用户每次都登录。我已将此... 查看详情

如何使用 Google Calendar API iOS Swift 创建事件

】如何使用GoogleCalendarAPIiOSSwift创建事件【英文标题】:HowtoCreateeventsusingGoogleCalendarAPIiOSSwift【发布时间】:2016-05-0407:41:48【问题描述】:我想开发一个演示应用程序,它将使用我的应用程序创建事件,使用GoogleCalendarAPI存储它,... 查看详情

使用 google API 发送电子邮件时出现“unauthorized_client”错误

】使用googleAPI发送电子邮件时出现“unauthorized_client”错误【英文标题】:"unauthorized_client"errorwhenusinggoogleAPItosendanemail【发布时间】:2021-10-1818:05:52【问题描述】:运行代码时出现此错误:google.auth.exceptions.RefreshError:(\'unau... 查看详情

如何使用 Google Cloud 调度程序 Python api 创建作业

】如何使用GoogleCloud调度程序Pythonapi创建作业【英文标题】:HowtocreateajobwithGoogleCloudschedulerPythonapi【发布时间】:2020-06-2603:08:55【问题描述】:我想创建一个每天上午10点运行的cron作业以触发云功能。但是,我遇到了Pythonapi的问... 查看详情

尝试使用 google-api-ruby-client 更新 CurrentLocation 的 Google Latitude 返回“400 Invalid Value”

】尝试使用google-api-ruby-client更新CurrentLocation的GoogleLatitude返回“400InvalidValue”【英文标题】:TryingtoupdateGoogleLatitudeofCurrentLocationusinggoogle-api-ruby-clientreturns"400InvalidValue"【发布时间】:2012-08-2806:58:27【问题描 查看详情