Search code examples
phpgoogle-search-consoleservice-accountsgoogle-client

php | Google Webmaster API Service Account has no permission


I am using the following code to get the stats for my website from the search console:

$client = new Google_Client();
$creds = $client->loadServiceAccountJson('---JSON FILE---', 'https://www.googleapis.com/auth/webmasters.readonly');
$client->setAssertionCredentials($creds);


$service = new Google_Service_Webmasters($client);

$ressource = $service->searchanalytics;

$options = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$options->setStartDate('2015-11-11');
$options->setEndDate('2015-11-11');

try{
    $response = $ressource->query('http://www.example.com/', $options);
} catch (Exception $e){
    echo $e->getMessage();
}

I use a google service account to authorize the api call but it gives me the error:

Error calling POST https://www.googleapis.com/webmasters/v3/sites/http%3A%2F%2Fwww.example.com%2F/searchAnalytics/query: (403) User does not have sufficient permission for site 'http*://www.example.com/'. See also: https://support.google.com/webmasters/answer/2451999.

I also added the email-address of the service account to my search console property as an owner, but the error is still there. I dont know anymore, what i am doing wrong?


Solution

  • So the problem is being sovled!

    The Problem is at first u have to add the service account to the search console property, then give it permission and then make him to an owner and u dont get the permission error again!

    Found the Solution here: Google Webmasters API for Java returns empty site list