Google 索引 API 调用不断返回 403 代码

Google Indexing API calls keep returning 403 codes

每当我们从职位空缺提供者那里收到职位空缺时,我都会尝试将职位空缺发送到 Google 索引 API。然而,尽管设置了所有适当的权限,我仍然收到 403 状态代码。

我已关注“Prerequisites for the Indexing API" guide and created a Service account. In my case, I have 4 entries for the domain in Google Search Console, so I followed 并将服务帐户添加到所有 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;

但是我不知道这可能是什么原因。

使用此代码return每次调用都会收到“403”。在网上浏览了一段时间后,我似乎可以找到一个明确的答案,除了 'make sure the service account is an owner' 但如前所述,情况已经如此。

我希望有人能帮助我,因为我坚持这个的时间比我愿意承认的要长。

如果需要更多信息,我会很乐意更新我的回答。

编辑: 根据要求,这里是 $httpClient->post() 调用的完整错误消息 return。

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 发布到索引 API。为了不超过每天 250 个请求的限制,请确保排除不应再编入索引的职位空缺。