2

I'm trying to use the example program provided by Microsoft here to test out the Bing Image Search Service associated with their Cognitive Service offering on Azure. I type the code in character by character (of course, using my own API key) and I get the below error message when I execute the program:

Traceback (most recent call last):
File "x.py", line 9, in <module>
image_results = client.images.search(query=search_term)
File "/home/rsbrownjr/anaconda3/envs/ibing/lib/python3.6/site-packages/azure/cognitiveservices/search/imagesearch/operations/images_operations.py", line 485, in search
raise models.ErrorResponseException(self._deserialize, response)
  azure.cognitiveservices.search.imagesearch.models.error_response_py3.ErrorResponseException: Operation returned an invalid status code 'PermissionDenied'

I know without a doubt that I have the right API key put into the program. I'm on pricing tier S0 pay-as-you-go but I don't have any other options either. This has got to have a simple solution.

from azure.cognitiveservices.search.imagesearch import ImageSearchAPI
from msrest.authentication import CognitiveServicesCredentials

subscription_key = "MY API KEY HERE"
search_term = "canadian rockies"

client = ImageSearchAPI(CognitiveServicesCredentials(subscription_key))

image_results = client.images.search(query=search_term)

if image_results.value:
    first_image_result = image_results.value[0]
    print("Total number of images returned: {}".format(len(image_results.value)))
    print("First image thumbnail url: {}".format(
        first_image_result.thumbnail_url))
    print("First image content url: {}".format(first_image_result.content_url))
else:
    print("No image results returned!")
RiverRook
  • 144
  • 10

1 Answers1

1

I just tested the code, the code is fine. So you must provided the wrong API key.

enter image description here

Here is my test key 52c32221ceaa4f388b26a2b85fb79bff. It is a 7 days free trial key. I got it here.

enter image description here

Tony Ju
  • 14,891
  • 3
  • 17
  • 31
  • With your key, the code works like a champ. What the heck, then? I'm getting the keys right from the Azure portal! I thank you so much for enabling me to run a test against that key. I will definitely check this answer as the correct one but I'd like to leave it open for a short time to see if anyone can provide some additional troubleshooting tips for me. As I said, I don't know what other key I could provide - it's coming right from the Azure portal ... maybe my pricing tier is the issue? Shouldn't be, though. Plus, I'm the owner of the subscription. Strange. – RiverRook Aug 09 '19 at 15:46
  • As I suspected, it was something simple. I thought creating a Cognitive Services resource group was enough. It wasn't. I had to create a specific Bing Image Search Service. Going ahead and awarding answer due to nice gesture of letting me use trial API key. – RiverRook Aug 10 '19 at 00:25
  • Thanks to RiverRock - that was the key for me - though it's now called just Bing Search Service – keybits Sep 09 '20 at 16:08