【问题标题】:Webmaster Tools API网站管理员工具 API
【发布时间】:2016-07-14 13:30:12
【问题描述】:

我遇到了网站管理员工具 API 的问题。我试图使脚本没有用户交互并获取我需要的信息。我已经让 Google Analytics 与服务帐户一起使用,但网站管理员工具不支持此功能。

我目前正在尝试使用 API 服务器密钥,但是当我尝试查询它返回的 API 时:

代码 403: "用户没有足够的网站权限"

但是,我是我要查询的网站的所有者。我检查了 www.google.com/webmasters/tools/home 并且该站点已通过验证。

所以我问的是我的代码是否正确?

$KEY_FILE_LOCATION = "privateKey.json";
$viewID = "view id"; //Website View ID - including ga:
$scopes = array(
    "https://www.googleapis.com/auth/analytics.readonly",
    "https://www.googleapis.com/auth/webmasters.readonly"
);

$emailAddr = 'Email';  //Got off google developer console
$apiKey = 'API Server Key'; //Got off google developer console

//Create Google Client
$client = new Google_Client();
$client->setApplicationName('Logging in Attempt');
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes($scopes);
$client->setAccessType('offline');
$client->setDeveloperKey($apiKey);

//Creating Webmaster Service
$webmastersService = new Google_Service_Webmasters($client);

//Creating Request
$request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$request->setStartDate('2016-07-01');
$request->setEndDate('2016-07-10');
$request->setDimensions( array('query') );

$requestWebsite = 'Website'; //The website I have access to

//Querying Webmaster
$qsearch = $webmastersService->searchanalytics->query($requestWebsite, $request);

$rows = $qsearch->getRows();

echo "<pre>",print_r($rows),"</pre>";

【问题讨论】:

标签: php google-search-console


【解决方案1】:

找出我哪里出错了。代码很好,问题出在搜索控制台上。该网站已经过验证,但是我需要将服务帐户中的电子邮件地址添加到网站中,并对其进行限制访问。一旦在那里,脚本就可以工作了。

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    相关资源
    最近更新 更多