node.js - 我不断从 Bing 的 API 获取 "404 - Resource not found"

标签 node.js azure bing bing-api

我已遵循 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/

相关文章:

silverlight - 如何从应用程序启动 wp7 内置 Bing 搜索

javascript - Nodejs,通过回调关闭mongo db连接

javascript - Electron - 从菜单打开文件

node.js - Angular 2 中未找到 @types 模块

node.js - 如何创建 GitHub 操作以使用 Yarn 运行 Jest 测试?

azure - 如何在azure VM上公开HTTP端口

azure - 有没有办法使用 Slow Cheetah 来转换 Azure Worker Role 中的 app.config?

c# - 更快或并行地将数据加载到单个 Azure Blob 中

c# - Bing 代码搜索不起作用,导致 VisualStudio 崩溃

internet-explorer - Internet Explorer 的 SSLCipherSuite 问题