访问appsmarket / v2 / customerLicense - 错误"无效的OAuth使用者密钥"

时间:2015-10-01 22:21:57

标签: google-apps-marketplace

直到最近(即我相信几个月),我们才能使用我们的Oauth2消费者密钥和密钥成功地针对https://www.googleapis.com/appsmarket/v2/customerLicensehttps://www.googleapis.com/appsmarket/v2/licenseNotification进行GET请求。

这些请求现在失败,状态为401,以及以下正文:

{"error"=>{"code"=>401, "message"=>"Invalid OAuth consumer key", "errors"=>[{"message"=>"Invalid OAuth consumer key", "reason"=>"authError", "locationType"=>"header", "domain"=>"global", "location"=>"Authorization"}]}}

这是我们的Ruby代码,用于发出licenseNotification请求

  def self.google_apps_licenses(since=Time.zone.now-10.years)
    oauth_consumer = OAuth::Consumer.new(GOOGLE_APPS_MARKETPLACE_CONSUMER_KEY_V2, GOOGLE_APPS_MARKETPLACE_CONSUMER_SECRET_V2)
    access_token = OAuth::AccessToken.new(oauth_consumer)
    response = access_token.get("https://www.googleapis.com/appsmarket/v2/licenseNotification/#{GOOGLE_APPS_MARKETPLACE_APPLICATION_ID_V2}?timestamp=#{(since.to_i * 1000)}")
    result = JSON.parse(response.body)
  end

任何帮助将不胜感激。我们现在需要使用我们的证书签名还是使用JWT断言?奇怪的是,这在某一点上起作用然后停止了。

1 个答案:

答案 0 :(得分:0)

在Google的帮助下,我找到了这个问题的答案。

首先,这些API的范围是https://www.googleapis.com/auth/appsmarketplace.license(目前很难在文档中找到)。

知道,有两种方法可以为该范围生成access_token。

1)您可以通过授权该范围的标准Oauth流程发送App的“项目成员”。然后,您可以存储refresh_token,并在需要发出请求时使用它来生成access_token。但是,可以撤销refresh_tokens。

2)因此,最好的方法是使用服务帐户代表自己(而不是用户)生成access_token。要做到这一点,您只需要在发送https://accounts.google.com/o/oauth2/token的JWT声明集中取消“子”字段。

这是执行该操作的ruby代码:

def self.fetch_access_token_for_marketplace_api
    claim_set = {
      "iss" => GOOGLE_OAUTH2_SERVICE_ACCOUNT_EMAIL_ADDRESS,
      "scope" => "https://www.googleapis.com/auth/appsmarketplace.license",
      "aud" => "https://accounts.google.com/o/oauth2/token",
      "exp" => (Time.zone.now + 50.minutes).to_i,
      "iat" => (Time.zone.now).to_i
    }
    p12 = OpenSSL::PKCS12::new(GOOGLE_OAUTH2_SERVICE_KEY, "<OMITTED>")

    jwt = JWT.encode(claim_set, p12.key, "RS256")

    data = {
      :assertion => jwt,
      :grant_type => "urn:ietf:params:oauth:grant-type:jwt-bearer"
    }
    response = make_request(:post, "https://accounts.google.com/o/oauth2/token", data)
    result = JSON.parse(response.body)

    raise ServiceTokenFailedError, "Unable to refresh google service token #{result["error"]}: #{result["error_description"]}" if result["error"]
    return result["access_token"]   
end