Google Analytics-API mit Ausnahme "invalid_grant" mit Dienstkonto. Gleicher Code auf zwei Servern. Nur einer funktioniert

Ich frage die Analytics-API über einDienstkonto.

Ich habe den Code auf den Dev-Server geschrieben und es funktioniert ohne Probleme. Wenn derselbe Code auf dem Produktionsserver ausgeführt wird, wird Folgendes 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 dasselbe.

Der oAuth IETF-Entwurf (http://tools.ietf.org/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 = '[email protected]';
$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#!topic/gs-discussion/3y_2XVE2q7U%5B1-25%5D) Verwenden von 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, jedoch nicht auf dem Produktionsserver.

Ich bin völlig ahnungslos in diesem Fall. Hat das jemand gesehen / behoben?

Vielen Dank!

Antworten auf die Frage(4)

Ihre Antwort auf die Frage