从 NodeJS 访问 Azure API 时出现访问错误
Access Error while accessing Azure API from NodeJS
我正在尝试在我的 NodeJS 应用程序中使用 Azure 新闻搜索。路由器的代码在这里:
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;
});
我收到一个错误:
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.
我确定我的 API 密钥是正确的。代码示例直接来自 Azure 的快速入门指南。那里没有提到端点。我觉得我错过了什么,但无法弄清楚是什么。
提前感谢您的指导。
试试这个来指定你的端点:
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;
});
结果:
希望对您有所帮助。
我正在尝试在我的 NodeJS 应用程序中使用 Azure 新闻搜索。路由器的代码在这里:
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;
});
我收到一个错误:
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.
我确定我的 API 密钥是正确的。代码示例直接来自 Azure 的快速入门指南。那里没有提到端点。我觉得我错过了什么,但无法弄清楚是什么。
提前感谢您的指导。
试试这个来指定你的端点:
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;
});
结果:
希望对您有所帮助。