Google网站管理员工具API-无法获得我拥有的域的权限

哑光

我正在使用此库: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>

所以我做了以下事情:

  • Google Cloud Console中,我创建了一个项目,并启用了Google Search Console API和旧版API,以防万一:

在此处输入图片说明

  • 为该项目创建了一个服务帐户

在此处输入图片说明

  • 对于服务帐户,我创建了一个密钥:

在此处输入图片说明

生成了一个JSON文件,这是我从上面的PHP代码加载的文件

  • 在Google Search Console中,我有一个域属性

在此处输入图片说明

我通过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.

matteo

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google Analytics网站管理员工具权限不足

Google网站管理员工具不会为我的网站编制索引

Google网站管理员工具说我的XML网站地图“似乎是HTML页面”

如何从 Google 网站管理员工具中删除旧的子域 URL?

Google网站管理员工具不接受我的站点地图

Google网站管理员工具API

网站管理员工具无法检测到我网站上的结构化数据

网站管理员工具/default.htm中的/ blog(子域)404错误

网站管理员工具未提取我的Angle 7应用程序sitemap.xml

提取为Google网站管理员工具

Google网站管理员工具和重复的链接

查询网站管理员工具API的维度

如何使用Google网站管理员工具API访问用户数据

我可以为我不拥有的域的网站添加主机名吗?

Google Adword&Search Console(Google网站管理员工具)

在 gitlab ce 中使用 LetsEncrypt 和 Google 网站管理员工具

如何使用PHP以编程方式在Google网站管理员工具中提交站点地图?

网站管理员工具中的“作为 Google 抓取”显示“重定向”

如何从网站管理员工具api获取查询详细信息

我不拥有的Google Assistant操作访问API

Google Drives API-检索我拥有的文件

MS Graph API - 预订业务 - 添加具有“管理员”角色的员工创建“externalGuest”

Google Sheets & GAS:无法直接从工具 -> 脚本编辑器访问我拥有的 GAS 项目,只能通过搜索

当我向上移动Google开发人员工具窗口时,网站布局发生变化

Drupal +网站管理员工具:“此位置的站点地图不允许使用此URL。”

谷歌网站管理员工具的回复:文档已临时移动

网站管理员工具中的 opencart 3 站点地图提交问题

无法获得具有管理员权限的AWS上的Lambda函数

如何使用React JS上下文API正确存储和检索数据?我拥有的代码无法正常工作