azurebingbing-api

Can I use Bing Search API and Auto Suggest API at the same time?


I have been using Bing Search API for a long time, and now I hope to add Bing AutoSuggest API in my program so I input the same subscription Key as the Search API to get the AutoSuggest results. But the program gave an error:

{
    "error": {
        "code": "401",
        "message": "Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."
    }
}

Do I need to create another resource group in Azure for Autosuggest? But when I do so, the Azure displayed an error message of insufficient quota when deploying:

    {
    "status": "Failed",
    "error": {
        "code": "InsufficientQuota",
        "message": "Insufficient Quota"
    }
}

What should I do now to make Autosuggest available? btw, I am using an account of Azure for student.


Solution

  • Can I use Bing Search API and Auto Suggest API at the same time?

    Yes, you can use both Bing Search API and Auto Suggest API simultaneously.

    You can use the below Python code to get both the Bing Search API and Auto Suggest API simultaneously.

    Code:

    import http.client,json,requests
    
    subscriptionKey = 'Your-key'
    host = 'api.bing.microsoft.com'
    path = '/v7.0/Suggestions'  
    search_url = "https://api.bing.microsoft.com/v7.0/search"
    mkt = 'en-US'
    query = 'sai' 
    search_term = "water"
    
    params = '?mkt=' + mkt + '&q=' + query
    headers = {'Ocp-Apim-Subscription-Key': subscriptionKey} 
    
    headers1 = {"Ocp-Apim-Subscription-Key": subscriptionKey}
    params1 = {"q": search_term, "textDecorations": True, "textFormat": "HTML"}
    response1 = requests.get(search_url, headers=headers1, params=params1)
    response1.raise_for_status()
    search_results = response1.text
    print(json.dumps(json.loads(search_results), indent=4))
    
    conn = http.client.HTTPSConnection(host)
    conn.request ("GET", path + params, None, headers)
    response = conn.getresponse ()
    Auto_result = response.read ()  
    print("Autosuggest",json.dumps(json.loads(Auto_result), indent=4))
    

    Sample output:

    {
        "_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": "2023-07-21T04: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": "2023-07-20T16:26:00.0000000Z",
                    "language": "en",
                    "isNavigational": false
                }
       } 
      Autosuggest {
    "_type": "Suggestions",
    "queryContext": {
        "originalQuery": "sai"
    },
    "suggestionGroups": [
        {
            "name": "Web",
            "searchSuggestions": [
                {
                    "url": "https://www.bing.com/search?q=sai+pallavi&FORM=USBAPI",
                    "displayText": "sai pallavi",
                    "query": "sai pallavi",
                    "searchKind": "WebSearch"
                },
                {
                    "url": "https://www.bing.com/search?q=sai+baba&FORM=USBAPI",
                    "displayText": "sai baba",
                    "query": "sai baba",
                    "searchKind": "WebSearch"
                },
                {
                    "url": "https://www.bing.com/search?q=saina+nehwal&FORM=USBAPI",
                    "displayText": "saina nehwal",
                    "query": "saina nehwal",
                    "searchKind": "WebSearch"
                },
                {
                    "url": "https://www.bing.com/search?q=sail+share+price&FORM=USBAPI",
                    "displayText": "sail share price",
                    "query": "sail share price",
                    "searchKind": "WebSearch"
                },
                {
                    "url": "https://www.bing.com/search?q=saif+ali+khan&FORM=USBAPI",
                    "displayText": "saif ali khan",
                    "query": "saif ali khan",
                    "searchKind": "WebSearch"
                },
                {
                    "url": "https://www.bing.com/search?q=sailpoint&FORM=USBAPI",
                    "displayText": "sailpoint",
                    "query": "sailpoint",
                    "searchKind": "WebSearch"
                },
                {
                    "url": "https://www.bing.com/search?q=saint-gobain&FORM=USBAPI",
                    "displayText": "saint-gobain",
                    "query": "saint-gobain",
                    "searchKind": "WebSearch"
                },
                {
                    "url": "https://www.bing.com/search?q=saira+banu&FORM=USBAPI",
                    "displayText": "saira banu",
                    "query": "saira banu",
                    "searchKind": "WebSearch"
                }
            ]
        }
    ]}
    

    enter image description here

    Reference:

    1. Quickstart: Perform a search with Python - Bing Web Search API - Bing Search Services | Microsoft Learn
    2. Quickstart: Suggest search strings with Bing Autosuggest REST API and Python - Bing Search Services | Microsoft Learn