php|Google网站管理员API服务帐户没有权限


php | Google Webmaster API Service Account has no permission

我使用以下代码从搜索控制台获取我的网站的统计信息:

$client = new Google_Client();
$creds = $client->loadServiceAccountJson('---JSON FILE---', 'https://www.googleapis.com/auth/webmasters.readonly');
$client->setAssertionCredentials($creds);

$service = new Google_Service_Webmasters($client);
$ressource = $service->searchanalytics;
$options = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$options->setStartDate('2015-11-11');
$options->setEndDate('2015-11-11');
try{
    $response = $ressource->query('http://www.example.com/', $options);
} catch (Exception $e){
    echo $e->getMessage();
}

我使用谷歌服务帐户来授权api调用,但它给了我错误:

调用POST时出错https://www.googleapis.com/webmasters/v3/sites/http%3A%2F%2Fwww.example.com%2F/searchAnalytics/query:(403)用户对网站"http://www.example.com/"没有足够的权限。另请参阅:https://support.google.com/webmasters/answer/2451999.

我还将服务帐户的电子邮件地址作为所有者添加到了我的搜索控制台属性中,但错误仍然存在。我不知道了,我做错了什么?

所以问题已经解决了!

问题是,首先你必须将服务帐户添加到搜索控制台属性中,然后授予它权限,然后将其作为所有者,这样你就不会再次出现权限错误!

在此处找到解决方案:Google Webmasters API for Java返回空站点列表

检查该属性是否列在站点列表中:https://developers.google.com/webmaster-tools/v3/sites/list

双击属性url中的https和www。