【问题标题】:I keep getting "404 - Resource not found" from Bing's APIs我不断从 Bing 的 API 中收到“404 - 找不到资源”
【发布时间】: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


    【解决方案1】:

    看起来路径不正确。

    实际路径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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 2013-06-21
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      相关资源
      最近更新 更多