我想通过 - >获取gsc数据https://github.com/google/google-api-php-client
我是项目所有者 - > https://developers.google.com/
受限用户 - > https://www.google.com/webmasters/tools/home?hl=de
我在https://developers.google.com/创建了一个角色项目所有者的服务帐户,并将json存储在我的服务器上。
到目前为止我的代码=
// Fetch GoogleSearchConsole data and store it.
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . getcwd() . '/../my-service-account-creds.json');
$client = new Google_Client();
$client->setApplicationName('my-project');
$client->useApplicationDefaultCredentials();
$client->addScope([Google_Service_Webmasters::WEBMASTERS_READONLY, Google_Service_Webmasters::WEBMASTERS]);
$service = New Google_Service_Webmasters($client);
$query = New Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$query->setStartDate(strtotime('2018-01-15'));
$query->setEndDate(strtotime('2018-01-22'));
$query->setDimensions(['page']);
$hmm = $service->searchanalytics->query('http://www.mysite.io', $query);
响应总是
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "User does not have sufficient permission for site 'http://www.mysite.io/'. See also: https://support.google.com/webmasters/answer/2451999."
}
],
"code": 403,
"message": "User does not have sufficient permission for site 'http://www.mysite.io/'. See also: https://support.google.com/webmasters/answer/2451999."
}
}
如果我打电话
,我甚至会得到一个访问令牌$client->fetchAccessTokenWithAssertion($my_http_client);
编辑:我会测试这个答案并报告我是否取得了成功https://stackoverflow.com/a/20961008/4732699
答案 0 :(得分:0)
我必须以用户身份将我的服务帐户添加到我的网站管理员资源。