ruby - 如何授权使用 google-api-ruby-client

标签 ruby oauth google-api

我正在为 ruby​​ 使用 google api,但不知道如何开始,请给我一个 ABC 示例,非常感谢?

最佳答案

如果您要创建服务帐户应用程序以访问 Google Analytics。

  1. 通过 https://code.google.com/apis/console 向 Google 注册. 在 API Access 选项卡上,单击 Create client ID,选择 Service Account。存储 Google 将生成的 key 文件,并记住该 key 的密码。
  2. 这里有一些代码可以帮助您入门

    require 'rubygems'
    require 'google/api_client'
    
    api_client = Google::APIClient.new
    path_to_key_file ="/path/to/key/file-privatekey.p12"
    passphrase = "google_generated_password"
    key = Google::APIClient::PKCS12.load_key(path_to_key_file, passphrase)
    

一旦 key 可用,使用您的客户端 ID(API 控制台中的电子邮件)初始化断言者 和授权范围。

asserter = Google::APIClient::JWTAsserter.new(
   'super_long_client_id_from_api_console@developer.gserviceaccount.com',
   'https://www.googleapis.com/auth/analytics.readonly',
   key)

# To request an access token, call authorize:
api_client.authorization = asserter.authorize()
puts api_client.authorization.access_token

http://code.google.com/p/google-api-ruby-client/wiki/ServiceAccounts

关于ruby - 如何授权使用 google-api-ruby-client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7493499/

相关文章:

ruby - 在 Ubuntu 11.10 上安装 watir-webdriver 时出错

node.js - 删除 API 后调用的 csrf protecteion

node.js - FacebookToken错误: This authorization code has been used

javascript - "Save to Drive"按钮可以/需要手动清理吗?

python - 如何使用 Gmail API 检查收到的新邮件

ruby - 为什么 'p' 打印最后一个变量的值?

ruby - 如何使用 sort_by 方法根据输赢数据对球队进行排序

google-api - 如何在 Google Ads api 中设置请求 header

ruby - 使用 RVM 安装位于本地系统上的 Ruby

ios - Firebase Microsoft 登录无法在 Swift iOS 中获取访问 token