Google Indexing API调用不断返回403码

问题描述 投票:0回答:1

每当我们从空缺提供者处收到空缺时,我都会尝试将其发送给Google Indexing API。但是,尽管设置了所有适当的权限,我仍然收到403状态代码。

我已遵循“ Prerequisites for the Indexing API”指南并创建了一个服务帐户。就我而言,我在Google Search Console中有4个域条目,因此我按照MarcQuay's answer并将服务帐户添加到所有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”。在浏览了很长时间的互联网之后,我似乎可以找到一个明确的答案,除了“确保服务帐户是所有者”之外,但正如前面所述,情况已经如此。

我希望有人可以帮助我,因为我在这个问题上停留的时间比我想承认的要长。

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

EDIT:

根据请求,这是$ 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: []
  }
}

每当我们从空缺提供者处收到空缺时,我都会尝试将其发送给Google Indexing API。但是,尽管设置了所有适当的权限,我仍然收到403状态...

php google-api-php-client google-indexing-api
1个回答
0
投票

我最终通过使用服务帐户作为AuthConfig来解决此问题

© www.soinside.com 2019 - 2024. All rights reserved.