使用 Google 索引 API 时出现 403 错误
Getting a 403 error when using Google Indexing API
我正在尝试通过 Google 索引 API 发送 POST 请求,但一直收到 HTTP 错误 403(禁止访问)。
我的代码如下:
require_once '../google-api-php-client-2.2.2/vendor/autoload.php';
$client = new Google_Client();
$client->setDeveloperKey('xxxxxxxxxxxxxx');
$client->setAuthConfig('xxxxxxxxxxxxx.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish?key=xxxxxxxxxx';
$content = "{
\"url\": \"https://www.example.com/whatever.html\",
\"type\": \"URL_UPDATED\"
}";
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
die('I am done...: ' . $status_code);
我的完整回复如下:
GuzzleHttp\Psr7\Response Object
(
[reasonPhrase:GuzzleHttp\Psr7\Response:private] => Forbidden
[statusCode:GuzzleHttp\Psr7\Response:private] => 403
[headers:GuzzleHttp\Psr7\Response:private] => Array
(
[Vary] => Array
(
[0] => X-Origin
[1] => Referer
[2] => Origin,Accept-Encoding
)
[Content-Type] => Array
(
[0] => application/json; charset=UTF-8
)
[Date] => Array
(
[0] => Sat, 09 Feb 2019 23:13:30 GMT
)
[Server] => Array
(
[0] => ESF
)
[Cache-Control] => Array
(
[0] => private
)
[X-XSS-Protection] => Array
(
[0] => 1; mode=block
)
[X-Frame-Options] => Array
(
[0] => SAMEORIGIN
)
[X-Content-Type-Options] => Array
(
[0] => nosniff
)
[Alt-Svc] => Array
(
[0] => quic=":443"; ma=2592000; v="44,43,39"
)
[Accept-Ranges] => Array
(
[0] => none
)
[Transfer-Encoding] => Array
(
[0] => chunked
)
)
[headerNames:GuzzleHttp\Psr7\Response:private] => Array
(
[vary] => Vary
[content-type] => Content-Type
[date] => Date
[server] => Server
[cache-control] => Cache-Control
[x-xss-protection] => X-XSS-Protection
[x-frame-options] => X-Frame-Options
[x-content-type-options] => X-Content-Type-Options
[alt-svc] => Alt-Svc
[accept-ranges] => Accept-Ranges
[transfer-encoding] => Transfer-Encoding
)
[protocol:GuzzleHttp\Psr7\Response:private] => 1.1
[stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream Object
(
[stream:GuzzleHttp\Psr7\Stream:private] => Resource id #84
[size:GuzzleHttp\Psr7\Stream:private] =>
[seekable:GuzzleHttp\Psr7\Stream:private] => 1
[readable:GuzzleHttp\Psr7\Stream:private] => 1
[writable:GuzzleHttp\Psr7\Stream:private] => 1
[uri:GuzzleHttp\Psr7\Stream:private] => php://temp
[customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
(
)
)
)
我明白 "Forbidden" 的意思,但我不明白为什么我会收到错误消息。我已按照设置说明进行操作。我已经设置了 API 键。我什至在开始时没有限制地设置它(当然我稍后会限制它),我已经设置了服务帐户密钥,json 文件存在于我调用它的地方(没有它,我得到一个 401错误,不是 403)并查看使用 API 密钥的帮助,我正确地传递了密钥。
我错过了什么?
如果您收到 403 错误,您很可能只需要在 Google Search Console 中添加您作为所有者使用的服务帐户。
您可以找到说明和/或信息here
首先启用 API。可能是以下错误帮助:
“错误”:{
“代码”:403,
"message": "Indexing API has not been used in project xxxxxxxxxx before or it is disabled. 通过访问 https://console.developers.google.com/apis/api/indexing.googleapis.com/overview?project=XXXXXXXXX 启用它然后重试。如果你最近启用了这个 API,请稍等将操作传播到我们的系统并重试的分钟数。",
我正在尝试通过 Google 索引 API 发送 POST 请求,但一直收到 HTTP 错误 403(禁止访问)。
我的代码如下:
require_once '../google-api-php-client-2.2.2/vendor/autoload.php';
$client = new Google_Client();
$client->setDeveloperKey('xxxxxxxxxxxxxx');
$client->setAuthConfig('xxxxxxxxxxxxx.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish?key=xxxxxxxxxx';
$content = "{
\"url\": \"https://www.example.com/whatever.html\",
\"type\": \"URL_UPDATED\"
}";
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
die('I am done...: ' . $status_code);
我的完整回复如下:
GuzzleHttp\Psr7\Response Object
(
[reasonPhrase:GuzzleHttp\Psr7\Response:private] => Forbidden
[statusCode:GuzzleHttp\Psr7\Response:private] => 403
[headers:GuzzleHttp\Psr7\Response:private] => Array
(
[Vary] => Array
(
[0] => X-Origin
[1] => Referer
[2] => Origin,Accept-Encoding
)
[Content-Type] => Array
(
[0] => application/json; charset=UTF-8
)
[Date] => Array
(
[0] => Sat, 09 Feb 2019 23:13:30 GMT
)
[Server] => Array
(
[0] => ESF
)
[Cache-Control] => Array
(
[0] => private
)
[X-XSS-Protection] => Array
(
[0] => 1; mode=block
)
[X-Frame-Options] => Array
(
[0] => SAMEORIGIN
)
[X-Content-Type-Options] => Array
(
[0] => nosniff
)
[Alt-Svc] => Array
(
[0] => quic=":443"; ma=2592000; v="44,43,39"
)
[Accept-Ranges] => Array
(
[0] => none
)
[Transfer-Encoding] => Array
(
[0] => chunked
)
)
[headerNames:GuzzleHttp\Psr7\Response:private] => Array
(
[vary] => Vary
[content-type] => Content-Type
[date] => Date
[server] => Server
[cache-control] => Cache-Control
[x-xss-protection] => X-XSS-Protection
[x-frame-options] => X-Frame-Options
[x-content-type-options] => X-Content-Type-Options
[alt-svc] => Alt-Svc
[accept-ranges] => Accept-Ranges
[transfer-encoding] => Transfer-Encoding
)
[protocol:GuzzleHttp\Psr7\Response:private] => 1.1
[stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream Object
(
[stream:GuzzleHttp\Psr7\Stream:private] => Resource id #84
[size:GuzzleHttp\Psr7\Stream:private] =>
[seekable:GuzzleHttp\Psr7\Stream:private] => 1
[readable:GuzzleHttp\Psr7\Stream:private] => 1
[writable:GuzzleHttp\Psr7\Stream:private] => 1
[uri:GuzzleHttp\Psr7\Stream:private] => php://temp
[customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
(
)
)
)
我明白 "Forbidden" 的意思,但我不明白为什么我会收到错误消息。我已按照设置说明进行操作。我已经设置了 API 键。我什至在开始时没有限制地设置它(当然我稍后会限制它),我已经设置了服务帐户密钥,json 文件存在于我调用它的地方(没有它,我得到一个 401错误,不是 403)并查看使用 API 密钥的帮助,我正确地传递了密钥。
我错过了什么?
如果您收到 403 错误,您很可能只需要在 Google Search Console 中添加您作为所有者使用的服务帐户。
您可以找到说明和/或信息here
首先启用 API。可能是以下错误帮助:
“错误”:{ “代码”:403, "message": "Indexing API has not been used in project xxxxxxxxxx before or it is disabled. 通过访问 https://console.developers.google.com/apis/api/indexing.googleapis.com/overview?project=XXXXXXXXX 启用它然后重试。如果你最近启用了这个 API,请稍等将操作传播到我们的系统并重试的分钟数。",