我们正在使用具有域范围授权的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."
}
我尝试将google-api-ruby-client
和googleauth
宝石更新到最新版本。没有改变。
我还尝试(两次)生成新的服务帐户,为DWD启用它,并在G Suite管理控制台中添加所需的范围和服务帐户的客户端ID。在管理控制台中添加范围后,我等待了24小时,没有任何更改。
请注意,过去一切都运行正常,设置相同。
这是抛出错误的最小代码。
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
据我所知,我们根据现有文档做了一切。有人有什么建议吗?
我还尝试在同一个组织下创建一个新的Google Cloud Platform项目,只添加了服务帐户,但一切都保持不变。
所以我终于找到了问题所在。在其他范围中,我也启用了这些:
http://www.google.com/m8/feeds/contacts/
http://www.google.com/m8/feeds/groups/
结果谷歌可能使他们失效,他们现在需要https
而不是http
。
如果他们提供更好的错误消息会很好,例如在Google管理控制台中授权时输入的范围无效。
我在尝试为与服务帐户使用的相同作用域授权正常OAuth2客户端ID时发现了错误。 OAuth2同意屏幕显示正确的错误消息。
以下是我为解决服务帐户错误而创建的清单:
unauthorized_client
错误。