Search code examples
phpsymfonygoogle-apigoogle-api-php-clientgoogle-indexing-api

Google Indexing API - 403 'Forbidden Response'


I am making a call to the google indexing API for job postings:

private $client;
private $httpClient;

public function initClient($kernel)
{
    $this->client = new \Google_Client();
    $this->client->setAuthConfig(JSON_KEY_HERE);
    $this->client->addScope('https://www.googleapis.com/auth/indexing');
    $this->httpClient = $this->client->authorize();
}

public function sendJob()
{
    $endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
    $content = "{
                    \"url\": \"URL_HERE\",
                    \"type\": \"URL_UPDATED\"
                }";

    $response = $this->httpClient->post($endpoint, array('body' => $content));
}

When making the call to the API, the response given is '403 - Forbidden' .

Any ideas what this error actually means? I have created the service account correctly but cannot replicate success from my dev enviroment.


Solution

  • Make sure that you have added the service account as an Owner in Google Search Console as described here: https://developers.google.com/search/apis/indexing-api/v3/prereqs#verify-site.

    My problem was that we had multiple entries for the domain in the Search Console (with and without www and with and without https); after adding the service account as an owner to all 4 entries it's working.