php - Google Indexing API 调用不断返回 403 代码
问题描述
每当我们从空缺提供者那里收到空缺时,我都会尝试将空缺发送到 Google Indexing API。然而,尽管设置了所有适当的权限,我仍然收到 403 状态代码。
我已遵循“索引 API 的先决条件”指南并创建了一个服务帐户。就我而言,我在 Google Search Console 中有 4 个域条目,因此我按照MarcQuay 的回答并将服务帐户添加到所有 4 个条目中。
我使用以下代码来实现 Google API 客户端并进行调用。对于我们收到的每个空缺,都会调用 sendGoogleRequest() 方法。
// Google API setup
function sendGoogleRequest(array $aData, int $sStatus = 0)
{
global $DOMAIN, $GOOGLE_AUTH_CONFIG, $GOOGLE_AUTH_SCOPE, $GOOGLE_API_ENDPOINT;
$googleClient = new Google_Client();
$googleClient->useApplicationDefaultCredentials();
$googleClient->addScope([$GOOGLE_AUTH_SCOPE]);
// Update Google Indexing API. This will notify Google that the URL should be crawled again.
$httpClient = $googleClient->authorize();
$endpoint = $GOOGLE_API_ENDPOINT;
$sJobUrl = $DOMAIN . '/vacancies/' . $aData['url'];
$sType = "";
if (!empty($sStatus)) {
switch ($sStatus) {
case 1:
$sType = "URL_UPDATED";
break;
case 2:
$sType = "URL_DELETED";
break;
}
}
$content = "{
\"url\": \"$sJobUrl\",
\"type\": \"$sType\"
}";
$response = $httpClient->post($endpoint, ['body' => $content]);
$status_code = $response->getStatusCode();
return $status_code;
}
我试过调试它,似乎 $googleClient->authorize() 中的 '$credentials' 是空的
$authHandler = $this->getAuthHandler();
if ($credentials) {
$callback = $this->config['token_callback'];
$http = $authHandler->attachCredentials($http, $credentials, $callback);
} elseif ($token) {
$http = $authHandler->attachToken($http, $token, (array) $scopes);
} elseif ($key = $this->config['developer_key']) {
$http = $authHandler->attachKey($http, $key);
}
return $http;
但是我不知道这可能是什么原因。
使用此代码为每次调用返回一个“403”。在浏览了一段时间的互联网之后,我似乎可以找到一个明确的答案,除了“确保服务帐户是所有者”,但如前所述,情况已经如此。
我希望有人可以帮助我,因为我被困在这个问题上的时间比我想承认的要长。
如果需要更多信息,我很乐意更新我的答案。
编辑:根据请求,这是 $httpClient->post() 调用返回的完整错误消息。
Response {#268 ▼
-reasonPhrase: "Forbidden"
-statusCode: 403
-headers: array:11 [▼
"Vary" => array:3 [▼
0 => "X-Origin"
1 => "Referer"
2 => "Origin,Accept-Encoding"
]
"Content-Type" => array:1 [▼
0 => "application/json; charset=UTF-8"
]
"Date" => array:1 [▼
0 => "Tue, 24 Sep 2019 11:25:29 GMT"
]
"Server" => array:1 [▼
0 => "ESF"
]
"Cache-Control" => array:1 [▼
0 => "private"
]
"X-XSS-Protection" => array:1 [▼
0 => "0"
]
"X-Frame-Options" => array:1 [▼
0 => "SAMEORIGIN"
]
"X-Content-Type-Options" => array:1 [▼
0 => "nosniff"
]
"Alt-Svc" => array:1 [▼
0 => "quic=":443"; ma=2592000; v="46,43,39""
]
"Accept-Ranges" => array:1 [▼
0 => "none"
]
"Transfer-Encoding" => array:1 [▼
0 => "chunked"
]
]
-headerNames: array:11 [▼
"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: "1.1"
-stream: Stream {#256 ▼
-stream: stream resource @123 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
}
解决方案
我最终通过使用服务帐户作为 AuthConfig 解决了这个问题
// Initialize Google_Client to submit vacancies to Indexing API
$googleClient = new Google_Client();
$googleClient->setAuthConfig($GOOGLE_AUTH_CONFIG);
$googleClient->addScope([$GOOGLE_AUTH_SCOPE]);
$googleClient->setAccessType("offline");
$googleClient 用于每次调用
function sendGoogleRequest(array $aData, int $sStatus = 0, Google_Client $google_Client) {
global $DOMAIN;
$sJobUrl = 'https://MY-DOMAIN.com/' . $aData['url'];
$sType = "";
if (!empty($sStatus)) {
switch ($sStatus) {
case 1:
$sType = "URL_UPDATED";
break;
case 2:
$sType = "URL_DELETED";
break;
}
}
$urlNotification = new Google_Service_Indexing_UrlNotification();
$urlNotification->setType($sType);
$urlNotification->setUrl($sJobUrl);
$indexingService = new Google_Service_Indexing($google_Client);
$response = $indexingService->urlNotifications->publish($urlNotification);
return $response;
}
然后,该脚本每天调用一次,并将每个 URL 发布到 Indexing API。为了不超过每天 250 个请求的限制,请确保排除不应再编入索引的空缺。
推荐阅读
- tensorflow - 将 tf.keras.layers 与 Tensorflow 低级 API 相结合
- asp.net-mvc - 由于 [ValidateAntiForgeryToken()] 和禁用的 Cookie,提交按钮没有点击控制器
- r - 如何在R中找出每个月有多少个交易日?
- python - 无法解决python中的意图错误
- python-3.x - TypeError: __init__() 得到了一个意外的关键字参数 'n_folds',sentiment_analysis_with_SVM
- python - 将 .ui 转换为 .py 文件,其中包含提升的框架
- python - 在win32com中按属性覆盖setter
- javascript - 如何在循环中从具有不同名称的不同属性中获取数据?
- javascript - 为什么我的函数没有获取道具价值?
- sql - 如何让此查询输出表中的多个值