javascript - 访问 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));
我收到错误
类型错误:获取失败
解决方案
CUSTOM_CONFIG_ID
在https://www.customsearch.ai/中查找。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
。
推荐阅读
- android - Android 上的 Firebase Crashlytics 不会在应用重新启动时保留用户 ID
- oracle - Oracle SQL*Loader WHEN 子句引发错误 2
- python - 如何使用熊猫数据框中的列表?
- postgresql - 如何在 Terraform 上使用 CRD 创建 crunchydata postgres 集群?
- r - 贝叶斯元分析和异质性中的森林图
- mongodb - Unifi Controller 在 Centos7 上停止
- python - Pytest 不接受以 ../../ 开头的参数
- sockets - Google PUB/SUB 通过 C 套接字连接发布消息
- autodesk-forge - 在 Forge Viewer 中查看 Recap Pro 项目
- html - 用css设置div的显示