如何解决访问 Bing 自定义 Web 搜索 API v7 时出现的问题
我正在尝试访问 Bing 自定义搜索 API。自定义搜索实例已设置并能够从 BING 门户(生产选项卡)调用 API,但是当我尝试通过 JS 访问相同的 URL 时。我收到失败的请求,如下所示
以下是我访问 API 的方式:
const query = "app";
const url = `https://api.bing.microsoft.com/v7.0/custom/search?q=${query}&customconfig=<CUSTOM_CONFIG_ID>&mkt=zh-CN`;
const option = {
mode: "cors",headers: {
"Ocp-Apim-Subscription-Key": <Subsription Key>
}
};
fetch(url,option)
.then((res) => res.json())
.then((data) => console.log(data))
.catch((err) => console.log(err));
我收到错误
类型错误:无法获取
解决方法
-
在https://www.customsearch.ai/中找到
CUSTOM_CONFIG_ID
。 -
在门户上找到
Ocp-Apim-Subscription-Key
。
const fetch = require("node-fetch");
const query = "app";
const url = `https://api.bing.microsoft.com/v7.0/custom/search?q=${query}&customconfig=<CUSTOM_CONFIG_ID>&mkt=zh-CN`;
const option = {
mode: "cors",headers: {
"Ocp-Apim-Subscription-Key": '<Subsription Key>'
}
};
fetch(url,option)
.then((res) => res.json())
.then((data) => console.log(data))
.catch((err) => console.log("err: " + err));
我的测试结果:
请先运行 node install node-fetch
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。