goryef
goryef

Reputation: 1489

Access Error while accessing Azure API from NodeJS

I am trying to use Azure News Search in my NodeJS App. The code for the router is here:

const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
let credentials = new CognitiveServicesCredentials('apikey');
let search_term = 'Winter Olympics'
const NewsSearchAPIClient = require('azure-cognitiveservices-newssearch');
let client = new NewsSearchAPIClient(credentials);
client.newsOperations.search(search_term).then((result) => {
    console.log(result.value);
}).catch((err) => {
    throw err;
});

I get an error:

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 made sure my API key is correct. The code sample is straight from Azure's Quickstart quide. There is no mention of the endpoint there. I feel like I am missing something but can't figure out what.

Thanks in advance for any guidance.

Upvotes: 1

Views: 190

Answers (1)

Stanley Gong
Stanley Gong

Reputation: 12153

Try this to specify your endpoint :

const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
let credentials = new CognitiveServicesCredentials('<api key>');
let search_term = 'Winter Olympics'
const NewsSearchAPIClient = require('azure-cognitiveservices-newssearch');


let client = new NewsSearchAPIClient(credentials,{"endpoint":"<endpoint url>"});

client.newsOperations.search(search_term,{"count":1}).then((result) => {
    console.log(result.value);
}).catch((err) => {
    console.log(err);
    throw err;
});

Result :

enter image description here

Hope it helps .

Upvotes: 1

Related Questions