首页 > 解决方案 > 访问 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));

我收到错误

类型错误:获取失败

标签: javascriptmicrosoft-cognitivebingbing-apibing-search

解决方案


  1. CUSTOM_CONFIG_IDhttps://www.customsearch.ai/中查找。

  2. 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

在此处输入图像描述


推荐阅读