Ich habe meinen Dienstaccount erstellt, die private_key- und delegierte domänenweite Berechtigung erhalten.Google OAuth 2.0 mit Dienstkonto mit PHP: "Ungültiger Token-Formatfehler"
Hier ist mein Code mit dem Dienstkonto zu authentifizieren versucht, aber die gleichen „Invalid Token Formatfehler“ bekommen:
session_start();
include_once 'google-api-php/vendor/autoload.php';
function getClient() {
$client = new Google_Client();
$client->setApplicationName('theName');
$client->setScopes('https://www.googleapis.com/auth/admin.directory.user.readonly');
$client->setAccessType('offline');
$client->setSubject('[email protected]');
$client->setAuthConfig('private_key.json');
// Load previously authorized credentials from a file.
$credentialsPath = 'private_key.json';
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
}
else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
Und hier ist Screenshot von dem, was ich von $ accessToken bekomme kurz vor
$client->setAccessToken($accessToken);
mit dem Fehler selbst:
https://postimg.org/image/ajgan5y27/
Jede Hilfe wird geschätzt. Vielen Dank!
Dies sollte als die richtige Antwort akzeptiert werden. – Alexey