客户端无权使用此方法检索访问令牌

客户端无权使用此方法检索访问令牌

本文介绍了客户端无权使用此方法检索访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ruby on Rails 项目中有一个自定义仪表板,其中包含从 Google Analytics 收集的数据.我使用 Google Analytics Reporting V4 API 和服务帐户进行身份验证.

I have a custom dashboard in Ruby on Rails project with data collected from Google Analytics. I user Google Analytics Reporting V4 API and a Service Account to authenticate.

如果我不模拟用户 authorization.sub = '[email protected]',我的代码运行良好,如果我这样做,我会收到 unauthorized_client 错误,但是不是所有的时候.有时有效,有时无效.

My code works well if I don't impersonate user authorization.sub = '[email protected]' and If I do it, I get unauthorized_client error but not all the times. Sometimes it works, and sometimes not.

这是我的代码:

scope = [Google::Apis::AnalyticsreportingV4::AUTH_ANALYTICS_READONLY]

view_id = 'xxxxxx'

ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "#{Rails.root}/private/google_analytics_key.json"
authorization = Google::Auth::get_application_default(scope)
authorization.sub = '[email protected]'

analytics = Google::Apis::AnalyticsreportingV4::AnalyticsReportingService.new
analytics.authorization = authorization

...

request = Google::Apis::AnalyticsreportingV4::GetReportsRequest.new(report_requests: report_requests)

@result = analytics.batch_get_reports(request)

Rails 服务器日志输出:

Rails server log output:

Sending HTTP post https://analyticsreporting.googleapis.com/v4/reports:batchGet?
Caught error Authorization failed.  Server message:
{
 "error": "unauthorized_client",
 "error_description": "Client is unauthorized to retrieve access tokens using this method."
}
Error - #<Signet::AuthorizationError: Authorization failed.  Server message:
{
 "error": "unauthorized_client",
 "error_description": "Client is unauthorized to retrieve access tokens using this method."
}>

Completed 500 Internal Server Error in 149ms (ActiveRecord: 0.4ms)



Signet::AuthorizationError (Authorization failed.  Server message:
{
 "error": "unauthorized_client",
 "error_description": "Client is unauthorized to retrieve access tokens using this method."
}):

推荐答案

请记住,服务帐户是一个虚拟用户.要使服务帐户能够访问您的 google 分析帐户,它需要获得预授权.您可以像对任何其他用户一样授权服务帐户.您需要通过 Google Analytics 网站将服务帐户添加为用户,您需要在 ACCOUNT 级别执行此操作,它必须是 ACCOUNT 级别.

Remember that a service account is a dummy user. For a service account to be able to access your google analytics account it needs to be preauthorized. You authorize the service account just like you would any other user. You will need to add the service account as a user via the Google Analytics website you need to do this at the ACCOUNT level it must be the ACCOUNT level.

就模拟而言,我不确定我是否理解您在做什么.我不是 ruby​​ 开发人员,但您的代码看起来不像我找到的示例 此处.

I am not sure I understand what you are doing as far as impersonation is going. I am not a ruby dev but your code doesn't look like the sample I found here.

这篇关于客户端无权使用此方法检索访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:16