Как аутентифицировать google-indexing с помощью сервисной учетной записи на сайте Drupal - PullRequest
1 голос
/ 01 июля 2019

Я пытаюсь использовать API индексации Google на сайте Drupal. Я получаю сообщение об ошибке «Отказано в разрешении 403».

Я включил API, создал учетную запись службы, подтвердил право собственности на сайт с помощью мета-тега, установил права владельца учетной записи службы и загрузил библиотеку google-api-php-client, чтобы упростить задачу. Я положил файл php на сервер. Когда я перехожу на страницу, я получаю ошибку 403. В доступе отказано.

require_once DRUPAL_ROOT . '/google-api-php-client/vendor/autoload.php';

//Set up service account credentials
putenv('GOOGLE_APPLICATION_CREDENTIALS='. DRUPAL_ROOT . '/myjsonfile.json');

$client = new Google_Client();

// service_account_file.json is the private key that you created for your service account.
//-----USING DEFAULT CREDENTIALS SET BY JSON FILE INSTEAD-----
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/indexing');

// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';

// Define contents here. The structure of the content is described in the next step.
$content = "{
  \"url\": \"urlToUpdate\",
  \"type\": \"URL_UPDATED\"
}";

$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();

Редактировать: я также пытался использовать метод $ client-> setAuthConfig (), как показано в документации googles, показанной здесь: API индексации Google

Вот альтернативный код после Google API

require_once DRUPAL_ROOT . '/google-api-php-client/vendor/autoload.php';

$client = new Google_Client();

// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig('myjsonfile.json');
$client->addScope('https://www.googleapis.com/auth/indexing');

// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';

// Define contents here. The structure of the content is described in the next step.
$content = "{
  \"url\": \"urlToUpdate\",
  \"type\": \"URL_UPDATED\"
}";

$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
...