Bing Image Search API Error 401: Permission Denied even with correct Key

Robert Melikyan 6 Reputation points
2020-11-05T21:08:44.353+00:00

Recently tried using the Bing Image Search API functionality using this guide provided by Microsoft, using their own "binder" notebook with no luck. I ran it on my own Jupyter Notebook and server with the same result: a Client Error 401, Permission Denied. Other problems suggested the API key was input incorrectly, but after numerous testing this does not appear to be the case. Here is the full output of the Error callback:

HTTPError Traceback (most recent call last)
<ipython-input-6-c1121699867c> in <module>
4 params = {"q": search_term, "license": "public", "imageType": "photo"}
5 response = requests.get(search_url, headers=headers, params=params)
----> 6 response.raise_for_status()
7 search_results = response.json()

/srv/conda/envs/notebook/lib/python3.7/site-packages/requests/models.py in raise_for_status(self)
933
934 if http_error_msg:
--> 935 raise HTTPError(http_error_msg, response=self)
936
937 def close(self):

HTTPError: 401 Client Error: PermissionDenied for url: https://api.cognitive.microsoft.com/bing/v7.0/images/search?q=puppies&license=public&imageType=photo

Bing Image Search
Bing Image Search
A Bing service that supports searching for images and gets comprehensive results.
44 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,958 questions
{count} vote

4 answers

Sort by: Most helpful
  1. YutongTie-MSFT 46,991 Reputation points
    2020-11-06T22:24:16.393+00:00

    Hello,

    We have internal update for the URL, please use this URL and it should work: search_url = "https://api.bing.microsoft.com/v7.0/images/search"

    38134-capture.jpg

    We are working on updating all samples and documents. Thanks a lot!

    Regards,
    Yutong

    1 person found this answer helpful.

  2. Robert Melikyan 6 Reputation points
    2020-11-08T19:17:33.547+00:00

    Hey @YutongTie-5848 , I am currently using the ImageSearchClient api and getting a 'Resource Not Found error' (full error posted below). Is only the REST API available or is the ImageSearchClient API for Python also available?

    ---------------------------------------------------------------------------
    ErrorResponseException                    Traceback (most recent call last)
    <ipython-input-5-f9ef654a8ee3> in <module>
          9         #results = search_images_google(google_key, f'{o}')
         10         #download_images(dest, urls=results.attrgot('link'))
    ---> 11         results = search_images_bing(ms_key, f'{o}')
         12         download_images(dest, urls=resutls.attrgot('content_url'))
         13 
    
    /notebooks/personal/climax/easter-egg/utils.py in search_images_bing(key, term, min_sz)
         31 def search_images_bing(key, term, min_sz=128):
         32     client = api("https://api.bing.microsoft.com/v7.0/images/search", auth(key))
    ---> 33     return L(client.images.search(query=term, count=150, min_height=min_sz, min_width=min_sz).value)
         34 
         35 def search_images_google(key, term, min_sz=128, start=1, num=10,exp=False, search_engine="1255cf7ee95a87668"):
    
    /opt/conda/envs/fastai/lib/python3.8/site-packages/azure/cognitiveservices/search/imagesearch/operations/_images_operations.py in search(self, query, accept_language, user_agent, client_id, client_ip, location, aspect, color, country_code, count, freshness, height, id, image_content, image_type, license, market, max_file_size, max_height, max_width, min_file_size, min_height, min_width, offset, safe_search, size, set_lang, width, custom_headers, raw, **operation_config)
        489 
        490         if response.status_code not in [200]:
    --> 491             raise models.ErrorResponseException(self._deserialize, response)
        492 
        493         deserialized = None
    
    ErrorResponseException: Operation returned an invalid status code 'Resource Not Found'
    
    0 comments No comments

  3. Robert Melikyan 6 Reputation points
    2020-11-08T19:17:33.58+00:00

    Hey @YutongTie-5848 , I am currently using the ImageSearchClient api and getting a 'Resource Not Found error' (full error posted below). Is only the REST API available or is the ImageSearchClient API for Python also available?

    ---------------------------------------------------------------------------
    ErrorResponseException                    Traceback (most recent call last)
    <ipython-input-5-f9ef654a8ee3> in <module>
          9         #results = search_images_google(google_key, f'{o}')
         10         #download_images(dest, urls=results.attrgot('link'))
    ---> 11         results = search_images_bing(ms_key, f'{o}')
         12         download_images(dest, urls=resutls.attrgot('content_url'))
         13 
    
    /notebooks/personal/climax/easter-egg/utils.py in search_images_bing(key, term, min_sz)
         31 def search_images_bing(key, term, min_sz=128):
         32     client = api("https://api.bing.microsoft.com/v7.0/images/search", auth(key))
    ---> 33     return L(client.images.search(query=term, count=150, min_height=min_sz, min_width=min_sz).value)
         34 
         35 def search_images_google(key, term, min_sz=128, start=1, num=10,exp=False, search_engine="1255cf7ee95a87668"):
    
    /opt/conda/envs/fastai/lib/python3.8/site-packages/azure/cognitiveservices/search/imagesearch/operations/_images_operations.py in search(self, query, accept_language, user_agent, client_id, client_ip, location, aspect, color, country_code, count, freshness, height, id, image_content, image_type, license, market, max_file_size, max_height, max_width, min_file_size, min_height, min_width, offset, safe_search, size, set_lang, width, custom_headers, raw, **operation_config)
        489 
        490         if response.status_code not in [200]:
    --> 491             raise models.ErrorResponseException(self._deserialize, response)
        492 
        493         deserialized = None
    
    ErrorResponseException: Operation returned an invalid status code 'Resource Not Found'
    

  4. YutongTie-MSFT 46,991 Reputation points
    2020-12-09T11:39:13.503+00:00

    Hello everyone,

    We have new version SDK is releasing, so everything should be perfect soon. For now, please try following corrections which should fix this error:

    https://api.cognitive.microsoft.com/bing/v7.0 => https://api.bing.microsoft.com/v7.0/
    o For example https://api.cognitive.microsoft.com/bing/v7.0/search => https://api.bing.microsoft.com/v7.0/search
    https://api.cognitive.microsoft.com/bingcustomsearch/v7.0 => https://api.bing.microsoft.com/v7.0/custom
    o For example https://api.cognitive.microsoft.com/bingcustomsearch/v7.0/search => https://api.bing.microsoft.com/v7.0/custom/search
    o For example https://api.cognitive.microsoft.com/bingcustomsearch/v7.0/images/search => https://api.bing.microsoft.com/v7.0/custom/images/search

    Please let me know if above not fixes your error. Thanks. I will also share the details once I know the new releasing date.

    Regards,
    Yutong

    0 comments No comments