我已遵循 Microsoft 的指南,但我只是不断收到带有错误的 JSON(响应如下)。
这是我按照 Microsoft 的官方指南进行的尝试:https://learn.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://learn.microsoft.com/en-us/bing/search-apis/bing-image-search/how-to/get-images
关于node.js - 我不断从 Bing 的 API 获取 "404 - Resource not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66164171/