使用Google API域范围授权时未经授权的客户端错误

问题描述 投票:3回答:1

我们正在使用具有域范围授权的Google API Ruby客户端来更新用户的联系人,解析电子邮件以将它们与我们的数据库等配对。在4月1日之前一切正常,但是,从那时起我们得到unauthorized_client错误响应。

Signet::AuthorizationError (Authorization failed.  Server message:)
{
  "error": "unauthorized_client",
  "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested."
}

What have I already tried?

我尝试将google-api-ruby-clientgoogleauth宝石更新到最新版本。没有改变。

我还尝试(两次)生成新的服务帐户,为DWD启用它,并在G Suite管理控制台中添加所需的范围和服务帐户的客户端ID。在管理控制台中添加范围后,我等待了24小时,没有任何更改。

请注意,过去一切都运行正常,设置相同。

Minimal code for test

这是抛出错误的最小代码。

require 'googleauth'
require 'google/apis/gmail_v1'

creds = ENV['GOOGLE_SERVICE_ACCOUNT'] # JSON downloaded from cloud console
                                      # is saved in this ENV variable
creds_json = JSON.parse(creds)
creds_json_io = StringIO.new(creds_json.to_json)
auth = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: creds_json_io,
  scope: [Google::Apis::GmailV1::AUTH_GMAIL_MODIFY]
)
auth.sub = '[email protected]' # without this line everything works fine
                                  # and I get the access token
auth.fetch_access_token

据我所知,我们根据现有文档做了一切。有人有什么建议吗?

Update

我还尝试在同一个组织下创建一个新的Google Cloud Platform项目,只添加了服务帐户,但一切都保持不变。

ruby google-api google-api-client google-authentication google-auth-library-ruby
1个回答
1
投票

所以我终于找到了问题所在。在其他范围中,我也启用了这些:

http://www.google.com/m8/feeds/contacts/ 
http://www.google.com/m8/feeds/groups/ 

结果谷歌可能使他们失效,他们现在需要https而不是http

如果他们提供更好的错误消息会很好,例如在Google管理控制台中授权时输入的范围无效。

我在尝试为与服务帐户使用的相同作用域授权正常OAuth2客户端ID时发现了错误。 OAuth2同意屏幕显示正确的错误消息。

Update

以下是我为解决服务帐户错误而创建的清单:

  1. 检查Google Cloud Console中的服务帐户是否启用了域范围委派。
  2. 检查您是否在Google管理控制台中使用服务帐户的客户端ID(而不是其电子邮件!)输入了适当的范围。确保所有范围都有效!他们可能在一生中变得无效。当您输入无效范围时,即使对于有效范围,在获取访问令牌时也会收到unauthorized_client错误。
© www.soinside.com 2019 - 2024. All rights reserved.