Where to look for source of error ErrorResponseException: Operation returned an invalid status code 'PermissionDenied'?

I am getting the following error when running the initial code in Chapter 2:

<function fastbook.search_images_bing(key, term, min_sz=128)>
results = search_images_bing(key, ‘grizzly bear’)
ims = results.attrgot(‘content_url’)

ErrorResponseException Traceback (most recent call last)
----> 1 results = search_images_bing(key, ‘grizzly bear’)
2 ims = results.attrgot(‘content_url’)
3 len(ims)

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastbook/init.py in search_images_bing(key, term, min_sz)
50 def search_images_bing(key, term, min_sz=128):
51 client = api(‘https://api.cognitive.microsoft.com’, auth(key))
—> 52 return L(client.images.search(query=term, count=150, min_height=min_sz, min_width=min_sz).value)
54 def plot_function(f, tx=None, ty=None, title=None, min=-2, max=2, figsize=(6,4)):

/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)
490 if response.status_code not in [200]:
–> 491 raise models.ErrorResponseException(self._deserialize, response)
493 deserialized = None

ErrorResponseException: Operation returned an invalid status code ‘PermissionDenied’

I searched for this error in Microsoft’s Azure site and in Paperspace with no results. So, where would I find information about this error so that I can resolve the problem?


I’m having the same problem.

I also have this issue. I was able to download the images when I was on my 7 day free trial. Once I’ve upgraded to pay as you go (still using it for free with the F1 free pricing tier), I’m receiving this error.

It doesn’t seem that anyone is really paying attention to our problems. I’m going to try to think of some way of testing to see exactly what the problem may be because we’re not getting the success code, that “200” value.

Problem is in function search_images_bing, it implies an endpoint within bing img search API which became obsolete.

Here is an answer to the problem:

OK, tried it but now I get the following error:

HTTPError                                 Traceback (most recent call last)
<ipython-input-5-8e0fcbc5ddbe> in <module>
----> 1 results = search_images_bing(key, 'grizzly bear', min_sz=128)
  2 ims = results.attrgot('contentUrl')
  3 len(ims)

<ipython-input-4-8e050447e4a0> in search_images_bing(key, term, max_images, **kwargs)
  4     search_url = "https://api.bing.microsoft.com/v7.0/images/search"
  5     response = requests.get(search_url, headers=headers, params=params)
----> 6     response.raise_for_status()
  7     search_results = response.json()
  8     return L(search_results['value'])

/opt/conda/envs/fastai/lib/python3.8/site-packages/requests/models.py in raise_for_status(self)
940         if http_error_msg:
--> 941             raise HTTPError(http_error_msg, response=self)
943     def close(self):

HTTPError: 401 Client Error: PermissionDenied for url:     https://api.bing.microsoft.com/v7.0/images/search?q=grizzly+bear&count=100

Clicking on the url link brings up the following error message:

{"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."}}

I opened my Azure account and tried again with the keys provided there and all fail I assume due to the fact that the endpoint is no longer Cognitive Services. So, I clicked Bing Resources and am not sure how to add that as an endpoint. I assume that we need to add a Bing Resource, correct?

NOTE: I don’t know how to do this yet, when I click Add I get the following error:


Could not create the marketplace item

This marketplace item is not available.

If i figure this out, I will post the answer.

Clear as mud. OK, Create Bing Search resource through Azure Marketplace discusses what the warning at What is the Bing Image Search API? warns about, that the Bing Search APIs are moving from Cognitive Services to Bing Search Services.

Searching https://portal.azure.com/#allservices for Bing Search or Bing Custom Search results in zero results, but there is Bing Resources, so clicking that brings you to the Bing Resources page that when the directions are followed, results in that Oops! message.

  • Clicking Learn More opens the Azure Cognitive Services page, which we were just told will not provide the Bing Search APIs!
  • Clicking Documentation opens the Azure Cognitive Services documentation page that now requires me to try to locate any information about using the photo collection as discussed in the fast.ai course.

I will keep looking and document what I’ve tried to prevent others from having to reach these dead ends. Perhaps finding another collection to use is in order, I’ll check that out too.

OK, i was provided with this link on the Microsoft site’s forum:


that leads to:


and recreated my Bing account and voila! it worked. This gets by the “Oops” failure message. Give it a try.

1 Like

See my reply to rania, with the links that avoid the “Oops” message.

I had the same issue only in paperspace not in colab. Also search_images_ddg was not working as utils.py was not imported. It worked by adding at the beginning

from utils import *

Hope this helps


Thanks this worked, strange that error was not saying anything about missing import.

I’ve re-visited the fast.ai course now that V4 is out. I’ve chosen the Paperspace/Gradient option to host the course material. I’m encountering issues with trying to get the Bing Search API set up.
The Azure portal is significantly different to that shown in the video. Bing API does not appear to exist and be an option available in the Azure portal to set up. The closet I can get to is Azure Cognitive Services and setting up an subscription. I manage to get a key but then I keep getting 401 errors around incorrect API key or end point. I am spending all my time trying to get my environment set up and doing nothing in learning. Any ideas? I’m going back to running Ubuntu server in the cloud otherwise.

1 Like

hi @periculo , please don’t use the bing API. Jeremy doesn’t recommend it anymore. You can use the following function to download pics from duckduckgo

from fastcore.all import *
import time

def search_images(term, max_images=200):
    url = 'https://duckduckgo.com/'
    res = urlread(url,data={'q':term})
    searchObj = re.search(r'vqd=([\d-]+)\&', res)
    requestUrl = url + 'i.js'
    params = dict(l='us-en', o='json', q=term, vqd=searchObj.group(1), f=',,,', p='1', v7exp='a')
    urls,data = set(),{'next':1}
    while len(urls)<max_images and 'next' in data:
        data = urljson(requestUrl,data=params)
        requestUrl = url + data['next']
    return L(urls)[:max_images]

HTTP403ForbiddenError: HTTP Error 403: Forbidden :sweat: