John Davidson

php - Google Webmaster Tools API - can't obtain permissions for a domain I own

0 comments
Message:


I'm using this library: https://github.com/googleapis/google-api-php-client


I created a simple PHP script that fetches some information from Google Search Console / Webmaster Tools about the performance of a domain I own.


It is not intented to autentify on behalf of a user who visits the page and own their own website. It's intended to show information about MY website and it identifies via a fixed service account. More detail below.


Here's the PHP code:


<!DOCTYPE html>
<html>
<head>
<title>Google Webmaster Tools test</title>
</head>
<body>
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

require_once ('./path/to/the/autoload.php');

echo "Instantiating client<br>";

$client = new Google_Client();

$credentials = file_get_contents ('./test_search_console_credentials.json', true) ;

echo "Configuring client<br>";

$client->setAuthConfig(json_decode($credentials,true));

$client->setScopes(['https://www.googleapis.com/auth/webmasters.readonly']);

$client->setAccessType('offline');

echo "Instantiating Webmasters service <br>";

$service = new Google_Service_Webmasters($client);


$query = new \Google_Service_Webmasters_SearchAnalyticsQueryRequest();

$date_now = date('Y-m-d');
$date_past = date('Y-m-d', time()-3600*24*30);


$query->setStartDate($date_past);
$query->setEndDate($date_now);
$query->setDimensions(['page','query']);
$query->setSearchType('web');


$pfilter=new \Google_Service_Webmasters_ApiDimensionFilter();
$pfilter->setDimension('page');
$pfilter->setOperator('equals');
$pfilter->setExpression('https://example.com/');


$fgroup= new Google_Service_Webmasters_ApiDimensionFilterGroup();
$fgroup->setFilters([$pfilter]);

$query->setDimensionFilterGroups([$fgroup]);

try {
$u = $service->searchanalytics->query('https://example.com', $query);
echo '<table border=1>';
echo '<tr>
<th>#</th><th>Clicks</th><th>CTR</th><th>Imp</th><th>Page</th><th>KEYword</th><th>Avg. pos</th>';
for ($i = 0; $i < count($u->rows); $i++) {
echo "<tr><td>$i</td>";
echo "<td>{$u->rows[$i]->clicks}</td>";
echo "<td>{$u->rows[$i]->ctr}</td>";
echo "<td>{$u->rows[$i]->impressions}</td>";
echo "<td>{$u->rows[$i]->keys[0]}</td>";
echo "<td>{$u->rows[$i]->keys[1]}</td>";
echo "<td>{$u->rows[$i]->position}</td>";
echo "</tr>";
}
echo '</table>';
} catch(\Exception $e ) {
echo $e->getMessage();
}
?>


</body>
</html>

So I did the following:



  • In Google Cloud Console I created a project and I enabled the Google Search Console API and also the Legacy one just in case:




  • I created a Service Account for the project:




  • For the Service Account, I created a key:



That generated a JSON file which is the one I load from the PHP code above.



  • In Google Search Console I have a domain property



whose ownership I verified via DNS. As you can see, I can see the data in the web interface.



  • I granted access to the abovementioned Service Account's email address with FULL access



Yet, when I try the script above, I get the error:


{ "error": { "code": 403, "message": "User does not have sufficient permission for site 'https://********.com'. See also: https://support.google.com/webmasters/answer/2451999.", "errors": [ { "message": "User does not have sufficient permission for site 'https://*******.com'. See also: https://support.google.com/webmasters/answer/2451999.", "domain": "global", "reason": "forbidden" } ] } }

Obviously the linked Help Center page doesn't help.


I have done the exact same thing with half a dozen other websites, all with the exact same procedure, with the same project, Service Account and credentials, and it works with most of them.


The only difference I can see between the properties with which it works and those with which it doesn't, is that the ones that work are properties that have existed for years, way before I even created the Google Cloud Platform project; and they were not "domain" properties, but url-prefix-based ones, that is, properties that either start with https:// or plain http://, either with or without the www., and they only work with the proper protocol and the right www prefix or lack thereof. And in those, you verify the ownership by uploading a file with a given path and name that Google gives you. However, I have tried to create a property like that for the newer domain that I'm trying to get to work, and that doesn't work either.


It's almost as if Google's api refused to work with properties created after a certain date.


NOTE: don't tell me I need Owner permissions instead of Full. I have already tried that (which you can only do through an older interface difficult as hell to find) and it makes no difference whatsoever. The older websites work both with Full and Owner permissions; the newer one doesn't work with either.



My Answer:
It seems like you have followed the correct steps in setting up the Google Webmaster Tools API and granting permissions to the Service Account. However, it's possible that there may be some issue with the domain property itself or how it was verified.

Here are a few things you can try to troubleshoot the issue:

1. Double-check the domain property in Google Search Console to ensure that it is properly verified and that the ownership is correctly assigned.

2. Make sure that the domain property is added to the correct Google Cloud Platform project where the Service Account is created.

3. Check if there are any specific restrictions or settings in the domain property that may be causing the permission issue.

4. Try creating a new domain property for the same website and see if you can obtain permissions for that property.

5. Contact Google Support for Webmaster Tools API to see if they can provide any insights or assistance in resolving the permission issue.

It's also worth checking the Google Webmaster Tools API documentation for any specific requirements or limitations related to domain properties. Sometimes, there may be certain restrictions or conditions that need to be met in order to access data for a specific domain property.

If none of the above steps resolve the issue, it may be helpful to reach out to Google Support for further assistance in troubleshooting the problem with obtaining permissions for the domain property.

Rate this post

4 of 5 based on 1478 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us