gpt4 book ai didi

node.js - 从 NodeJS 访问 Azure API 时出现访问错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:52:08 57 4
gpt4 key购买 nike

我正在尝试在我的 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 key 是正确的。该代码示例直接来自 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;
});

结果:

enter image description here

希望有帮助。

关于node.js - 从 NodeJS 访问 Azure API 时出现访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58938345/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com