为什么 Bing Web 搜索 API 返回代码 401?

问题描述 投票:0回答:1

{"code":"401","message":"由于订阅密钥无效或 API 端点错误,访问被拒绝。请确保为活动订阅提供有效密钥,并为您的资源使用正确的区域 API 端点。" }

我确认订阅密钥是正确的

azure
1个回答
0
投票

{"code":"401","message":"由于订阅密钥无效或 API 端点错误,访问被拒绝。请确保为活动订阅提供有效密钥,并为您的资源使用正确的区域 API 端点。" }

上述错误通常在传递错误的订阅密钥或错误的 API 端点时发生。

此外,请验证在 API 请求中传递密钥后是否重新生成了密钥。

我使用了下面的代码和正确的订阅密钥,它运行得很好。

代码:

import requests, json

subscription_key = "dxxxxf"
search_term = "water"

search_url = "https://api.bing.microsoft.com/v7.0/search"
headers = {"Ocp-Apim-Subscription-Key": subscription_key}
params = {"q": search_term, "textDecorations": True, "textFormat": "HTML"}
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.text
print(json.dumps(json.loads(search_results), indent=4))

输出:

{
    "_type": "SearchResponse",
    "queryContext": {
        "originalQuery": "water"
    },
    "webPages": {
        "webSearchUrl": "https://www.bing.com/search?q=water",
        "totalEstimatedMatches": 80500000,
        "value": [
            {
                "id": "https://api.bing.microsoft.com/api/v7/#WebPages.0",
                "contractualRules": [
                    {
                        "_type": "ContractualRules/LicenseAttribution",
                        "targetPropertyName": "snippet",
                        "targetPropertyIndex": 0,
                        "mustBeCloseToContent": true,
                        "license": {
                            "name": "CC-BY-SA",
                            "url": "http://creativecommons.org/licenses/by-sa/3.0/"
                        },
                        "licenseNotice": "Text under CC-BY-SA license"
                    }
                ],
                "name": "Water - Wikipedia",
                "url": "https://en.wikipedia.org/wiki/Water",
                "thumbnailUrl": "https://www.bing.com/th?id=OIP.lqPjgDbDBZliHqXtjKciCQHaE3&w=80&h=80&c=1&pid=5.1",
                "isFamilyFriendly": true,
                "displayUrl": "https://<b>en.wikipedia.org</b>/wiki/<b>Water</b>",
                "snippet": "<b>Water</b> is an inorganic compound with the chemical formula H 2 O. It is a transparent, tasteless, odorless, and nearly colorless chemical substance, and it is the main constituent of Earth&#39;s hydrosphere and the fluids of all known living organisms (in which it acts as a solvent). It is vital for all known forms of life, despite not providing food energy, or organic micronutrients.",
                "dateLastCrawled": "2024-05-23T04:14:00.0000000Z",
                "language": "en",
                "isNavigational": false
            },
            {
                "id": "https://api.bing.microsoft.com/api/v7/#WebPages.1",
                "name": "Water | Definition, Chemical Formula, Structure, Molecule, &amp; Facts ...",
                "url": "https://www.britannica.com/science/water",
                "thumbnailUrl": "https://www.bing.com/th?id=OIP.8FrlGPSeeeTBA6ZErHMl4AHaE7&w=80&h=80&c=1&pid=5.1",
                "isFamilyFriendly": true,
                "displayUrl": "https://<b>www.britannica.com</b>/science/<b>water</b>",
                "snippet": "Liquid <b>water</b>. <b>water</b> molecule. The <b>water</b> molecule is composed of two hydrogen atoms, each linked by a single chemical bond to an oxygen atom. Most hydrogen atoms have a nucleus consisting solely of a proton. Two isotopic forms, deuterium and tritium, in which the atomic nuclei also contain one and two neutrons, respectively, are found to a small ...",       
                "dateLastCrawled": "2024-05-23T16:26:00.0000000Z",
                "language": "en",
                "isNavigational": false
            }
   } 

enter image description here

参考:

快速入门:使用 Python 执行搜索 - Bing Web 搜索 API - Bing 搜索服务 |微软学习

© www.soinside.com 2019 - 2024. All rights reserved.