You need to enable JavaScript to run this app. 已授权自有域名的Service Account调用Google Webmaster Tools API报403错误
优惠活动
大模型
产品
解决方案
定价
更多

已授权自有域名的Service Account调用Google Webmaster Tools API报403错误

我使用的开发库为:https://github.com/googleapis/google-api-php-client

我编写了一个简单的PHP脚本,用于从Google Search Console(原Webmaster Tools)拉取我自有域名的搜索表现数据。

该脚本并非为访问页面的普通用户(其拥有自身站点)做身份验证,而是用于展示我自有站点的数据,通过固定的Service Account完成身份识别,细节如下。

以下是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(以防兼容问题):
    enter image description here

  • 为该项目创建了Service Account:
    enter image description here

  • 为上述Service Account创建了密钥:
    enter image description here

该操作生成的JSON文件即为上述PHP脚本加载的凭证文件。

  • 在Google Search Console中我持有一个Domain Property(域名属性),已通过DNS完成所有权验证,可在网页端正常查看该站点数据:
    enter image description here

  • 我已为上述Service Account的邮箱地址授予了*FULL(完全)*访问权限:
    enter image description here

但运行上述脚本时仍出现如下错误:

{
  "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"
      }
    ]
  }
}

显然,错误提示指向的帮助中心页面无法解决该问题。

我使用相同的项目、Service Account及凭证,对另外6个站点执行了完全相同的操作,其中大部分站点均可正常调用API。

我观察到的可正常运行的属性与无法运行的属性唯一差异在于:可正常运行的属性均为多年前创建的,远早于我创建Google Cloud Platform项目的时间;且这些属性并非Domain Property,而是基于URL前缀的属性,即属性前缀为https://或http://,带或不带www标识,仅在协议、www前缀完全匹配时生效,这类属性通常通过上传指定路径及名称的Google验证文件完成所有权验证。我尝试为当前要对接的新域名创建URL前缀类型的属性,但同样无法正常工作。

这几乎像是Google API拒绝对接某一日期之后创建的属性。

注:请勿告知我需要将权限从Full改为Owner,我已经尝试过该操作(需通过较难找到的旧版界面操作),但无任何效果。旧站点使用Full或Owner权限均可正常运行,而新站点使用两种权限均无法访问。


内容的提问来源于stack exchange,提问作者matteo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.12 19:54:03