Ich habe ein benutzerdefiniertes Dashboard im Ruby on Rails-Projekt mit Daten aus Google Analytics. Ich benutze Google Analytics Reporting V4 API und ein Dienstkonto zur Authentifizierung.Client ist nicht berechtigt, Zugriffstoken mit dieser Methode abzurufen
Mein Code funktioniert gut, wenn ich nicht Benutzer authorization.sub = '[email protected]'
imitieren und wenn ich es tue, bekomme ich unauthorized_client
Fehler, aber nicht alle Male. Manchmal funktioniert es und manchmal nicht.
Dies ist mein Code:
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-Server Log-Ausgabe:
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."
}):
Für den Google Analytics-API-Zugriff wird authorization.sub nicht unterstützt, da es sich bei Analytics nicht um eine GSuite-Anwendung (Apps for work) handelt. – Matt
Das gleiche Problem hier: http://stackoverflow.com/questions/41903578/google-api-service-account-authorization-error – MarcGuay