Ich rufe die Analytics-API über eine Dienstleistungskonto .
Ich habe den Code auf dem Dev-Server geschrieben und er funktioniert ohne Probleme. Wenn ich denselben Code auf dem Produktionsserver ausführe, wird dies ausgelöst:
Google_AuthException: Fehler beim Aktualisieren des OAuth2-Tokens, Meldung: '{ "error" : "invalid_grant" }'
Ich habe versucht, ein anderes Dienstkonto zu erstellen, und das Verhalten ist das gleiche.
Der oAuth-IETF-Entwurf ( https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-31 ) sagt dies über den Fehler:
invalid_grant
The provided authorization grant (e.g. authorization
code, resource owner credentials) or refresh token is
invalid, expired, revoked, does not match the redirection
URI used in the authorization request, or was issued to
another client.
Hier ist der Code, den ich geschrieben habe:
$GA_CLIENT_ID = 'XX.apps.googleusercontent.com';
$GA_APP_EMAIL = 'XX@developer.gserviceaccount.com';
$GA_APP_NAME = 'XX';
$GA_KEY_FILE = 'XX';
// create client object and set app name
$client = new Google_Client();
$client->setApplicationName($GA_APP_NAME); // name of your app
// set assertion credentials
$client->setAssertionCredentials(
new Google_AssertionCredentials(
$GA_APP_EMAIL, // email you added to GA
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents($GA_KEY_FILE) // keyfile you downloaded
));
// other settings
$client->setClientId($GA_CLIENT_ID); // from API console
$client->setAccessType('offline_access'); // this may be unnecessary?
// create service and get data
$service = new Google_AnalyticsService($client);
$result = $service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams);
return $result;
Ich habe auch eine hier vorgeschlagene Lösung ausprobiert ( https://groups.google.com/forum/?fromgroups#!thema/gs-diskussion/3y_2XVE2q7U%5B1-25%5D ) mit authenticatedRequest() anstelle von Google_AnalyticsService:
$req = new Google_HttpRequest($apiUrl);
$resp = $client::getIo()->authenticatedRequest($req);
$result = json_decode($resp->getResponseBody(), true);
Diese Alternative funktioniert auch auf dem Entwicklungsserver, aber nicht auf dem Produktionsserver.
In diesem Punkt bin ich völlig ratlos. Hat jemand dieses Problem gesehen/behoben?
Danke!