Search Console API returns Empty array for site list even though I have several sites in my search console.
I have several sites in my search console. When I use the api Explorer, they are returned in the array. Once I use the PHP or Python client library, get an empty result with my Service Account Credentials. This seems to be a permissions issue but the service account I am using has owner role permissions assigned to it, so that doesn't make sense. Any help would be appreciated!
This is my code:
$client = new Google_Client();
$credentials_file = 'path/to/file.json';
$client->setAuthConfig($credentials_file);
$client->setScopes(['https://www.googleapis.com/auth/webmasters']);
$service = new Google_Service_Webmasters($client);
var_dump($service->sites->listSites()->getSiteEntry());
My result: array(0) {} even though it have several websites in the search console.
Search console sites and site data is considered user data and cannot be accessed by service accounts. You have to make an Oauth 2 interface and save the token to a file and use that token in your app. Make sure the app refreshes the token when it expires:
if ($client->isAccessTokenExpired()) {
$client->refreshToken($this->token['refresh_token']);
$token = $client->getAccessToken(); // save this to a file
}