Я создал свою учетную запись службы, получил полномочия private_key и делегированных доменов.Google OAuth 2.0 с учетной записью службы с PHP: «Ошибка формата недопустимого формата токена»
Вот мой код попытка аутентификации с учетной записью службы, но получить ту же «ошибку Недопустимого маркера формата»:
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;
}
А вот скриншот того, что я получаю от $ маркера доступа непосредственно перед
$client->setAccessToken($accessToken);
с самой ошибки:
https://postimg.org/image/ajgan5y27/
Любая помощь будет оценена по достоинству. Благодаря!
Это должно быть принято как правильный ответ. – Alexey