Bing Image Search API Error 401: Permission Denied

ib_cheshire 26 Reputation points
2021-05-26T19:02:22.907+00:00

Hello

I am trying to follow the quickstart guide here for the image search API: https://video2.skills-academy.com/en-gb/bing/search-apis/bing-image-search/quickstarts/rest/python
However, I get this error: 401 Client Error: PermissionDenied for url: https://api.bing.microsoft.com/v7.0/images/search?q=puppies&license=public&imageType=photo
I have made various attempts to get this working based on answers to previous questions, but I cannot resolve it.

I have created a cognitive services and cognitive search resources, and used the keys from here. I have also just tried using my 'subscription' key from my main account.
What am I doing wrong?

Code below:

subscription_key = my_key
search_url = "https://api.bing.microsoft.com/v7.0/images/search"
search_term = "puppies"

headers = {"Ocp-Apim-Subscription-Key" : subscription_key}

params = {"q": search_term, "license": "public", "imageType": "photo"}

response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()
thumbnail_urls = [img["thumbnailUrl"] for img in search_results["value"][:16]]

Bing Image Search
Bing Image Search
A Bing service that supports searching for images and gets comprehensive results.
44 questions
{count} vote

Accepted answer
  1. romungi-MSFT 43,656 Reputation points Microsoft Employee
    2021-05-27T07:04:31.597+00:00

    @ib_cheshire Are you are using the resource keys of the bing resource that is available from the marketplace?

    100173-image.png

    I used the keys from this resource in this sample and it returned a response successfully.

    # Copyright (c) Microsoft Corporation. All rights reserved.  
    # Licensed under the MIT License.  
      
    # -*- coding: utf-8 -*-  
      
    import json  
    import os  
    from pprint import pprint  
    import requests  
      
    '''  
    This sample makes a call to the Bing Image Search API with a text query and returns relevant images with data.  
    Documentation: https: // video2.skills-academy.com/en-us/azure/cognitive-services/bing-web-search/  
    '''  
      
    # Add your Bing Search V7 subscription key and endpoint to your environment variables.  
    subscriptionKey = "<your_key>"  
    endpoint = "https://api.bing.microsoft.com/v7.0/images/search/"  
      
    # Query to search for  
    query = "puppies"  
      
    # Construct a request  
    mkt = 'en-US'  
    params = {'q': query, 'mkt': mkt}  
    headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}  
      
    # Call the API  
    try:  
        response = requests.get(endpoint, headers=headers, params=params)  
        response.raise_for_status()  
      
        print("\nHeaders:\n")  
        print(response.headers)  
      
        print("\nJSON Response:\n")  
        pprint(response.json())  
    except Exception as ex:  
        raise ex  
    

0 additional answers

Sort by: Most helpful