我正在使用此库:https : //github.com/googleapis/google-api-php-client
我创建了一个简单的PHP脚本,该脚本从Google Search Console /网站管理员工具中获取一些有关我所拥有的域的性能的信息。
并非旨在代表访问该页面并拥有自己的网站的用户来增强功能。它旨在显示有关我的网站的信息,并通过固定服务帐户进行标识。下面有更多详细信息。
这是PHP代码:
<!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>
所以我做了以下事情:
生成了一个JSON文件,这是我从上面的PHP代码加载的文件。
我通过DNS验证了其所有权。如您所见,我可以在Web界面中看到数据。
但是,当我尝试上面的脚本时,出现错误:
{ "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" } ] } }
显然,链接的“帮助中心”页面无济于事。
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.
I found the answer here: Webmasters API User does not have sufficient permission for site (MartijnvdB's answer in case the deep link to the specific answer breaks).
I needed to change this:
$u = $service->searchanalytics->query('https://example.com', $query);
to this:
$u = $service->searchanalytics->query('sc-domain:example.com', $query);
There are two types of Google Search Console properties: domain-type and prefix-type. For domain-type properties you need to pass the name of the property as https://www.example.com
(with http or https, with or without the www matching the exact prefix of the property). For domain-based properties you need to pass it as sc-domain:example.com
.
I had tried with just example.com
without any kind of prefix, but that didn't work. I don't know why the API can't just accept that and prepend sc-domain:
when needed.
The PHP library has no documentation whatsoever, and the documentation of the API itself is unbelievably poor too, as is usual for Google APIs.
Once you already know the answer, you can find it documented here:
https://developers.google.com/webmaster-tools/search-console-api-original/v3/searchanalytics/query
siteUrl字符串在Search Console中定义的属性的URL。示例:http : //www.example.com/(用于URL前缀属性)或sc-domain:example.com(用于Domain属性)
请注意,它显示为“在Search Console中定义”。以用户身份浏览Search Console时,您永远不会在sc-domain:...
任何地方看到该属性的名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句