Ответ 1
ok вот решение, которое я нашел.
Для ключа P12
Обычно для генерации токена используется следующий код.
public static function getToken()
{
$key = file_get_contents( APPLICATION_DIR.'/'.APPLICATION_GOOGLE_CLIENT_KEY_FILE);
$cred = new Google_Auth_AssertionCredentials(
APPLICATION_GOOGLE_CLIENT_EMAIL,
array('https://spreadsheets.google.com/feeds'),
$key
);
$client = new Google_Client();
$client->setAssertionCredentials($cred);
if (!$client->getAuth()->isAccessTokenExpired()) {
return false;
}
else {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service_token = json_decode($client->getAccessToken());
return $service_token->access_token;
}
Для ключа JSON
но теперь у нас нет ключа P12, у нас есть ключ JSON, поэтому я только что внес некоторые изменения в код выше, пожалуйста, посмотрите ниже:
public static function getToken()
{
$key = APPLICATION_DIR.'/'.APPLICATION_GOOGLE_CLIENT_KEY_FILE;
$data = json_decode(file_get_contents($key)); // here i decoded the json
if (isset($data->type) && $data->type == 'service_account') {
$cred = new Google_Auth_AssertionCredentials(
APPLICATION_GOOGLE_CLIENT_EMAIL, // it the client email
array('https://spreadsheets.google.com/feeds'), // it google spreadsheet scope
$data->private_key // here is the private key
);
}
$client = new Google_Client();
$client->setAssertionCredentials($cred);
if (!$client->getAuth()->isAccessTokenExpired()) {
return false;
}
else {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service_token = json_decode($client->getAccessToken());
return $service_token->access_token;
}
Что это....:):)