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

Ruby on rails 客户端无权使用此方法检索访问令牌,ruby-on-rails,google-analytics,google-api,google-analytics-api,service-accounts,Ruby On Rails,Google Analytics,Google Api,Google Analytics Api,Service Accounts,我在RubyonRails项目中有一个自定义仪表板,其中包含从Google Analytics收集的数据。我使用Google Analytics Reporting V4 API和服务帐户进行身份验证 如果我不模拟用户authorization.sub=,我的代码工作得很好xxx@mail.com“如果我这样做,我会得到未经授权的\u客户端错误,但并非总是如此。有时有效,有时无效 这是我的代码: scope = [Google::Apis::AnalyticsreportingV4::AUTH_

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

如果我不模拟用户
authorization.sub=,我的代码工作得很好xxx@mail.com“
如果我这样做,我会得到
未经授权的\u客户端
错误,但并非总是如此。有时有效,有时无效

这是我的代码:

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 = 'xxx@mail.com'

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服务器日志输出:

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."
}):
发送HTTP posthttps://analyticsreporting.googleapis.com/v4/reports:batchGet?
捕获错误授权失败。服务器消息:
{
“错误”:“未经授权的\u客户端”,
“错误描述”:“客户端无权使用此方法检索访问令牌。”
}
错误-#
149ms内完成500个内部服务器错误(ActiveRecord:0.4ms)
Signet::AuthorizationError(授权失败。服务器消息:
{
“错误”:“未经授权的\u客户端”,
“错误描述”:“客户端无权使用此方法检索访问令牌。”
}):

请记住,服务帐户是虚拟用户。为了让服务帐户能够访问您的google analytics帐户,需要对其进行预授权。您可以像授权任何其他用户一样授权服务帐户。您需要通过Google Analytics网站以用户身份添加服务帐户。您需要在帐户级别添加服务帐户,必须是帐户级别


就模仿而言,我不确定我是否理解你在做什么。我不是ruby开发人员,但您的代码与我找到的示例不一样

问题是我无法为服务帐户用户授予Google Analytics视图的读取权限。我不是管理员。。。我的问题是为什么它只是偶尔出现,我如何解决错误?也许谷歌开发者控制台中有一些配置?非常感谢。服务账户必须通过google analytics网站进行预授权,没有解决办法。如果您不是帐户的所有者,为什么要使用服务帐户,您应该使用Oauth2并请求所有者的访问。这可能有助于存储刷新令牌,以便能够随时访问用户数据。如果我执行Oauth2身份验证,我需要在谷歌分析视图中为每个需要访问权限的用户提供读取权限,但这不会发生。这很难解释,但我需要进行服务帐户身份验证。我不是管理员,但我拥有所有其他权限。如果您使用Oauth2,则需要为将使用您的应用程序的每个用户提供一个刷新令牌。刷新令牌将允许您访问所有google analytics帐户,用户说您有权访问这些帐户,但每个google analytics视图不需要一个。除非您可以联系每个用户并让他们授予您的服务帐户访问他们的google analytics帐户的权限,否则您不能使用服务帐户。您必须使用oauth2authorization.sub,Google Analytics API访问不支持,因为Analytics不是GSuite(工作应用程序)应用程序。此处存在相同问题: