首页 > 解决方案 > 使用 Google OAuth 2 API 刷新令牌时如何修复“帐户已被删除”

问题描述

最近,由于某种未知原因,我们的谷歌分析 API 无法正常运行。我知道这个问题围绕着一些帐户删除。例如,这里是刷新访问令牌的代码:

uri = URI.parse('https://www.googleapis.com/oauth2/v4/token')
params = {
  client_id: client_id,
  client_secret: client_secret,
  refresh_token: token,
  grant_type: 'refresh_token'
}
headers = {
  'Content-Type' => 'application/x-www-form-urlencoded'
}
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
post_request = Net::HTTP::Post.new(uri.path, headers)
query = URI.encode_www_form(params)
post_request.body = query
response = http.request(post_request)

响应返回此:

#<Net::HTTPBadRequest 400 Bad Request readbody=true>
response.body = "{\n  \"error\": \"invalid_grant\",\n  \"error_description\": \"Account has been deleted\"\n}"

我花了很长时间弄清楚如何解决这个问题。我需要重新创建什么帐户?服务帐户?文档真的不清楚。

标签: ruby-on-railsgoogle-analytics-apigoogle-oauth

解决方案


推荐阅读