I am trying to send a POST request via the Google Indexing API, but keeps getting HTTP erro 403 (Forbidden).
My code is below:
require_once '../google-api-php-client-2.2.2/vendor/autoload.php'; $client = new Google_Client(); $client->setDeveloperKey('xxxxxxxxxxxxxx'); $client->setAuthConfig('xxxxxxxxxxxxx.json'); $client->addScope('https://www.googleapis.com/auth/indexing'); $httpClient = $client->authorize(); $endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish?key=xxxxxxxxxx'; $content = "{ "url": "https://www.example.com/whatever.html", "type": "URL_UPDATED" }"; $response = $httpClient->post($endpoint, [ 'body' => $content ]); $status_code = $response->getStatusCode(); die('I am done...: ' . $status_code);
My full response is as follows:
GuzzleHttpPsr7Response Object ( [reasonPhrase:GuzzleHttpPsr7Response:private] => Forbidden [statusCode:GuzzleHttpPsr7Response:private] => 403 [headers:GuzzleHttpPsr7Response:private] => Array ( [Vary] => Array ( [0] => X-Origin [1] => Referer [2] => Origin,Accept-Encoding ) [Content-Type] => Array ( [0] => application/json; charset=UTF-8 ) [Date] => Array ( [0] => Sat, 09 Feb 2019 23:13:30 GMT ) [Server] => Array ( [0] => ESF ) [Cache-Control] => Array ( [0] => private ) [X-XSS-Protection] => Array ( [0] => 1; mode=block ) [X-Frame-Options] => Array ( [0] => SAMEORIGIN ) [X-Content-Type-Options] => Array ( [0] => nosniff ) [Alt-Svc] => Array ( [0] => quic=":443"; ma=2592000; v="44,43,39" ) [Accept-Ranges] => Array ( [0] => none ) [Transfer-Encoding] => Array ( [0] => chunked ) ) [headerNames:GuzzleHttpPsr7Response:private] => Array ( [vary] => Vary [content-type] => Content-Type [date] => Date [server] => Server [cache-control] => Cache-Control [x-xss-protection] => X-XSS-Protection [x-frame-options] => X-Frame-Options [x-content-type-options] => X-Content-Type-Options [alt-svc] => Alt-Svc [accept-ranges] => Accept-Ranges [transfer-encoding] => Transfer-Encoding ) [protocol:GuzzleHttpPsr7Response:private] => 1.1 [stream:GuzzleHttpPsr7Response:private] => GuzzleHttpPsr7Stream Object ( [stream:GuzzleHttpPsr7Stream:private] => Resource id #84 [size:GuzzleHttpPsr7Stream:private] => [seekable:GuzzleHttpPsr7Stream:private] => 1 [readable:GuzzleHttpPsr7Stream:private] => 1 [writable:GuzzleHttpPsr7Stream:private] => 1 [uri:GuzzleHttpPsr7Stream:private] => php://temp [customMetadata:GuzzleHttpPsr7Stream:private] => Array ( ) ) )
I understand what “Forbidden” means, but I can’t understand WHY I am getting the error. I have followed the instructions for setting up. I have set up the API key. I have even set it with no restrictions to start with (of course I will restrict it later), I have set up the service account keys, the json file exists where I call it (without it, I get a 401 error, not 403) and looking at the help for using API keys, I am passing the key correctly.
What am I missing?
Advertisement
Answer
If you are getting a 403 error, you most likely just need to add the service account you are using as an Owner in the Google Search Console.
You can find instructions and or information here