【发布时间】:2021-02-11 22:54:25
【问题描述】:
我已按照 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:
{“错误”:{“代码”:“404”,“消息”:“找不到资源”}}
在他们的文档('api.cognitive.microsoft.com')上找到的端点的结果是这个 JSON:
{"error": {"code": "401", "message": "由于订阅密钥无效或 API 端点错误导致访问被拒绝。确保为活动订阅提供有效密钥并使用正确的区域您的资源的 API 端点。"}}
我也用类似的代码尝试了 Bing 搜索 API - 但路径不同,我得到了同样的错误。
【问题讨论】:
标签: node.js azure bing bing-api