【问题标题】:Accessing appsmarket/v2/customerLicense - error "Invalid OAuth consumer key"访问 appsmarket/v2/customerLicense - 错误“无效的 OAuth 消费者密钥”
【发布时间】:2015-10-09 23:04:34
【问题描述】:

直到最近(即我相信是几个月),我们仅使用我们的 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"}]}}

这是我们生成 licenseNotification 请求的 Ruby 代码

  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 断言?奇怪的是,这在某一时刻起作用然后停止了。

【问题讨论】:

  • 我认为消费者密钥和秘密与 Oauth 1.0 一起使用,该 Oauth 1.0 已于 2015 年 4 月 20 日弃用并停止工作。您必须使用 Oauth 2.0。 support.google.com/a/answer/162105?hl=en
  • 嗨@gerado 谢谢你的回复!可能的愚蠢问题:应用程序如何发出不代表用户的 Oauth2 请求?
  • 要授权用户,您可以使用正常的 Oauth 2 流程。您必须创建凭据,并使用这些凭据对用户进行身份验证。查看相关文档:developers.google.com/identity/protocols/OAuth2 并查看 Oauth Playground,您可以在其中逐步查看流程:developers.google.com/oauthplayground

标签: google-apps-marketplace


【解决方案1】:

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

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

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

1) 您可以通过标准 Oauth 流程向该范围发送应用程序的“项目成员”。然后,您可以存储 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

【讨论】:

    猜你喜欢
    • 2021-09-02
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 2012-07-29
    • 2012-09-28
    • 2015-12-05
    相关资源
    最近更新 更多