php - Google indexing API wrong json file ← (PHP)

Hi I am trying to use Google indexing API. I used this tutorial - https://developers.google.com/search/apis/indexing-api/v3/prereqs

I used this php library - different branch - v1-master because I dont use composer so I had to use autoload.php https://github.com/googleapis/google-api-php-client/tree/master

Here is my code:

require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/src/Google/autoload.php');
require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/examples/templates/base.php');

$client = new Google_Client();

// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/serviceAccount');
$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": "http://example.com/jobs/42",
  "type": "URL_UPDATED"
}';

$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
  1. I created service account
  2. downloaded service account json file This is how the json looks

enter image description here

  1. added path to json as you can see above
  2. I enabled Indexing API in console.developers.google.com for my project
  3. json loads

But I got error Uncaught Google_Exception: Invalid client secret JSON file.

This topic is related to - google-api-php-client: Invalid client secret JSON file

But i just could not resolve it and I am stuck

Answer



Solution:

Issue solved, according to issue here - https://github.com/googleapis/google-api-php-client/issues/2025#issuecomment-762634717, autoload has been removed and whole package depends on autoloader from composer (if you are using it). If you are not using it, author pointed to the last usable version with autoloader here

https://github.com/googleapis/google-api-php-client/releases/tag/v2.8.2

Download this version and include only one autoload.php in your code

require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/vendor/autoload.php');

Whole code is here

require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/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($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/mykey-pre-10b793743427.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": "https://my-site.sk/news.php?ID=157",
  "type": "URL_UPDATED"
}';

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

Also dont forget to add client_email from your json into Search Console owners by clicking on Settings - Users and Permission - click on dots next to you account - click Manage owners - add there new Owner (adding it to users in the previous site is not enough) - you would get 403 code. If operation successful you would get Code 200, but in Search Console nothing would be changed because you initiated index job, that might run in few hours

Source