我正在尝试使用 Azure 的 Bing News API 创建一个成功的请求。以下来自文档的屏幕截图显示 Ocp-Apim-Subscription-Key 是必需的标头。https://docs.microsoft.com/en-us/rest/api/cognitiveservices-bingsearch/bing-news-api-v7-reference
我已经创建了一个帐户,根据这篇文章在访问 Bing 自定义 Web 搜索 API v7中的问题,可以在 Bing 资源 -> 密钥和端点中找到密钥:
我已经尝试了两个键,但都不起作用。由于订阅密钥无效或 API 端点错误,我收到错误代码 401 访问被拒绝。我注意到这张图片中的端点与 bing 新文档中列出的端点不同。我尝试了图片中列出的端点(只是为了查看),但出现 404 错误。
另一个线程说去 Azure 门户上的 API 管理。 https://docs.microsoft.com/en-us/answers/questions/62385/please-help-me-to-find-the-process-to-get-ampampam.html
导航到 API 管理菜单后,它显示“没有要显示的 API 管理服务”。我可以“创建 API 管理”,但随后的表格要求提供似乎非典型的信息才能访问 API。这真的是创建密钥的地方还是我做错了什么?谢谢你。
这是我的代码。我尝试了 Postman 并遇到了同样的错误。
import fetch from 'node-fetch';
function testFetch(){
let response = fetch("https://api.cognitive.microsoft.com/bing/v7.0/news/trendingtopics", {
headers: {
"Ocp-Apim-Subscription-Key": <redacted-key>,
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).then(async response => {
try {
const data = await response.json()
console.log('response data?', data)
} catch(error) {
console.log('Error happened here!')
console.error(error)
}
})
}
testFetch()