1

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.

4

1 回答 1

1

试试这个来指定你的端点:

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;
});

结果 :

在此处输入图像描述

希望能帮助到你 。

于 2019-11-20T09:10:57.173 回答