0
votes

I've followed Microsoft's guides, but I just keep getting a JSON with an error (responses are below).

Here's what I tried, following the official guide by Microsoft at: 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();

result for the 'api.bing.microsoft.com' endpoint is always this JSON:

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

result for the endpoint found on their docs ('api.cognitive.microsoft.com') is this 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."}}

I've also tried the Bing search API with a similar code - but a different path, and I got the same errors.

1

1 Answers

0
votes

Looks like the path is incorrect.

Actual path : https://api.bing.microsoft.com/v7.0/images/search

Used path : https://api.bing.microsoft.com/bing/v7.0/images/search

Try the below code :

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';

Changes in the above code :

bingpath has been modified from /bing/v7.0... to /v7.0/....

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