【问题标题】:How to authorize with the Google API Ruby client?如何使用 Google API Ruby 客户端进行授权?
【发布时间】:2017-05-16 17:40:59
【问题描述】:

我想通过 google 的 API 从我的 google 分析帐户中获取数据,并在我的一个仪表板上显示一些值。如果可能,我想为此使用 Ruby。

google-api-ruby-client 似乎是一个不错的起点,我什至发现了一些 useful sample code 有很大帮助,但我无法正确授权我的请求。

在同一个示例代码中,有一部分是shows how to do it,但我不确定从哪里获得必要的密钥。

到目前为止我做过的事情:

  1. https://console.developers.google.com 上创建了一个项目
  2. 启用分析 API
  3. 根据向导,我创建并下载了一个服务帐户密钥,如下所示:

    {
      "type": "service_account",
      "project_id": "xxx"
      "provate_key_id": "xxx",
      "private_key": "xxx",
      "client_email": "xxx",
      "client_id": "xxx",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://accounts.google.com/o/oauth2/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "xxx"
    }
    

但是,我不知道如何使用它。也许我犯了一个错误,我需要从 Google 获取其他类型的密钥?

【问题讨论】:

    标签: ruby api google-api google-analytics-api


    【解决方案1】:

    由于 Google API 的性质和企业安全要求,这比一般的 RESTful 硅谷 API 稍微复杂一些。我需要在过去的项目中这样做,我认为这会有所帮助。

    首先,为了更轻松地查询我需要的数据,我使用了Legato,它自称为“Google Analytics Core Reporting and Management API 的 Ruby 客户端”。

    为了让 Legato 正常工作,您需要从 Google 获取 OAuth 令牌,该令牌会在一段时间后过期。

    class AuthToken
      def self.retrieve
        new.retrieve
      end
    
      def retrieve(expires_in = 1.hour)
        client = Google::APIClient.new(application_name: 'YOUR APP NAME', application_version: '0.1')
        client.authorization = service_account('https://www.googleapis.com/auth/analytics.readonly', private_key).authorize
        OAuth2::AccessToken.new(oauth_client, client.authorization.access_token, expires_in: expires_in )
      end
    
    private
    
      def oauth_client
        OAuth2::Client.new('', '', {
          authorize_url: authorize_url,
          token_url: token_url
        })
      end
    
      def service_account(scope, key)
        Google::APIClient::JWTAsserter.new(ENV['GOOGLE_SERVICE_EMAIL'], scope, key)
      end
    
      def private_key
        @private_key ||= Google::APIClient::PKCS12.load_key(
          ENV['GOOGLE_PRIVATE_KEY_PATH'],
          ENV['GOOGLE_PRIVATE_KEY_PASSPHRASE']
        )
      end
    
      def authorize_url
        'https://accounts.google.com/o/oauth2/auth'
      end
    
      def token_url
        'https://accounts.google.com/o/oauth2/token'
      end
    end
    

    这假设您有三个与 Google 提供给您的身份验证数据相对应的环境变量:

    1. GOOGLE_SERVICE_EMAIL,与您在 JSON 对象中的客户端电子邮件相同。
    2. GOOGLE_PRIVATE_KEY_PATH,它指向您应该能够同时下载的.p12 文件。
    3. GOOGLE_PRIVATE_KEY_PASSPHRASE,字面意思应该是“notasecret”。

    您可以像这样使用连奏服务:

    class ArticlePageviews
      extend Legato::Model
      metrics :pageviews
      dimensions :page_path
      filter(:only_articles) { contains :page_path, '/articles/' }
    end
    
    ga_user = Legato::User.new(AuthToken.retrieve)
    ga_profile = ga_user.profiles.first
    
    ArticlePageviews.only_articles.results(ga_profile)
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2020-09-23
      • 2014-11-18
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      相关资源
      最近更新 更多