Получение "CREDENTIALS_PATH" в виде файла credentials.json при работе с Google Service

При попытке получить отзывы о компании привязанной к аккаунту google получаю ошибку в виде выброшенного исключения:

Google_Service_Exception: { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential" }}

Подключаюсь к API таким образом:

require_once "lib/Google/vendor/autoload.php";  
include_once "MyBusiness.php"; //Библиотека Google MyBusiness API
define('APPLICATION_NAME', 'User Query - Google My Business API');
define('CREDENTIALS_PATH', 'key/credentials.json'); // Файл пуст на данный момент
define('CLIENT_SECRET_PATH', 'key/client_secrets.json'); //Файл верный есть.
 
$redirect_uri = 'http://test';
 
$client = new Google_Client();
$client->setDeveloperKey("*Ключ разработчика для работы с API Google Service*"); //Также получен
$client->setApplicationName("Client_Library_Examples");
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
 
$client->addScope("https://www.googleapis.com/auth/plus.business.manage");

// For retrieving the refresh token
$client->setRedirectUri($redirect_uri); 
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
 
$mybusinessService = new Google_Service_MyBusiness($client);
$credentialsPath = CREDENTIALS_PATH;

if (isset($_GET['code'])) {
  $accessToken = $client->authenticate($_GET['code']);

  if (!file_exists(dirname($credentialsPath))) {
    mkdir(dirname($credentialsPath), 0700, true);
  }
  file_put_contents($credentialsPath, $accessToken);
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
 
// Load previously authorized credentials from a file.
if (file_exists($credentialsPath)) {
  $accessToken = file_get_contents($credentialsPath);
  $client->setAccessToken($accessToken);
  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    $client->refreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, $client->getAccessToken());
  }
} else {
  // Request authorization from the user.
  $authUrl = $client->createAuthUrl();
}
 
$accounts = $mybusinessService->accounts;
$accountsList = $accounts->listAccounts()->getAccounts();
$account = $accountsList[0];
$locations = $mybusinessService->accounts_locations;
$locationsList = $locations->listAccountsLocations($account->name)->getLocations();
$location = $locationsList[0];

$reviews = $mybusinessService->accounts_locations_reviews;
$listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);
$reviewsList = $listReviewsResponse->getReviews();

Подскажите, как получить файл: credentials.json, если ошибка вызвана все же его отсутствием.


Ответы (0 шт):