我一直从 Bing 的 API 收到“404 - 资源未找到”

I keep getting "404 - Resource not found" from Bing's APIs

我已经按照 Microsoft 的指南进行操作,但我总是收到 JSON 错误消息(回复如下)。

这是我按照 Microsoft 的官方指南尝试的方法:https://docs.microsoft.com/en-us/azure/cognitive-services/bing-image-search/quickstarts/nodejs

let https = require('https');
let bingSubscriptionKey = '[SUBSCRIPTIONKEY]'; // tried both keys I received on the Azure portal
let bingHost = 'api.bing.microsoft.com'; // this is the endpoint I was provided with on the Azure portal, I've also tried the endpoint on MS's guide: api.cognitive.microsoft.com
let bingPath = '/bing/v7.0/images/search';
let term='pizza';

let request_params = {
    method : 'GET',
    hostname : bingHost,
    path : bingPath + '?q=' + encodeURIComponent(term),
    headers : {
        'Ocp-Apim-Subscription-Key' : bingSubscriptionKey,
    }
};

let response_handler = function (response) {
    let responseBody = '';

    response.on('data', function (d) {
        responseBody += d;
    });
    
    response.on('end', function () {
        console.log ("responseBody", responseBody); // responseBody always contains a JSON with an error
    });
};

let req = https.request(request_params, response_handler);
req.end();

'api.bing.microsoft.com' 端点的结果始终是 JSON:

{"error": {"code": "404", "message": "Resource not found"}}

在他们的文档 ('api.cognitive.microsoft.com') 上找到的端点的结果是 JSON:

{"error": {"code": "401", "message": "Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}

我也尝试过 Bing 搜索 API 使用类似的代码 - 但路径不同,我得到了相同的错误。

看起来路径不正确。

实际路径 : https://api.bing.microsoft.com/v7.0/images/search

使用路径 : https://api.bing.microsoft.com/bing/v7.0/images/search

试试下面的代码:

let bingHost = 'api.bing.microsoft.com'; // this is the endpoint I was provided with on the Azure portal, I've also tried the endpoint on MS's guide: api.cognitive.microsoft.com
let bingPath = '/v7.0/images/search';
let term='pizza';

以上代码改动:

bingpath 已从 /bing/v7.0... 修改为 /v7.0/....

参考:https://docs.microsoft.com/en-us/bing/search-apis/bing-image-search/how-to/get-images