0
votes

I have created service account and below is my code:

    $client = new Google_Client();

    $client->setApplicationName("Console_Client");

    $client->setAuthConfigFile('xxaax.json');

    $scopes = [
        //"https://www.googleapis.com/auth/webmasters",
        //"https://www.googleapis.com/auth/webmasters.readonly"
        Google_Service_Webmasters::WEBMASTERS_READONLY,
        Google_Service_Webmasters::WEBMASTERS,
        ];

    $client->setScopes($scopes);

    $client->setAccessType('offline');

    $service = new Google_Service_Webmasters($client);

    $request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();

    $optParams = array('siteUrl' => 'https://www.test.co/');

    $request->startDate = "2018-01-01";

    $request->endDate   = "2018-01-02";

    $results = $service->searchanalytics->query($optParams, $request);

I'm receiving this error message in response:

PHP Fatal error: Uncaught Google_Service_Exception "reason": "insufficientPermissions"

I have enabled this api in my account and am also able to hit it when trying google API explorer.

Why is this occurring?

1

1 Answers

0
votes

For this we need to create service account. This is preferable in the case when you are hitting API from console and you don't have any web UI for OAuth2.0.

For this particular case, there is service account Id (looks like long string email). Copy this and add it to your google account. Doing above solved my problem.

This step was not clear in google API docs.

Hope this helps someone in future.