john
john

Reputation: 2544

Invalid Subscription Key for python code but same key is working fine on westus.dev.cognitive.microsoft.com

I am just getting started with microsoft's computer vision OCR api, Subscription key and image url is working fine on https://westus.dev.cognitive.microsoft.com/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fc/console

But I am getting the following error when using the python code

{"statusCode": 401, "message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription."}

I have tried my best to figure out the error but failed.

What am I doing wrong?

Thanks in advance.

import httplib, urllib, base64

headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': '{1111460aa78d4b27****************}',
}

params = urllib.urlencode({
    # Request parameters
    'language': 'unk',
    'detectOrientation ': 'true',
})

try:
    conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
    conn.request("POST", "/vision/v1.0/ocr?%s" % params, "{\"url\":\"https://s-media-cache-ak0.pinimg.com/originals/fb/e6/56/fbe65691cb66c6f035a859d9671c3fe5.jpg\"}", headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

Upvotes: 0

Views: 464

Answers (1)

cthrash
cthrash

Reputation: 2973

You need to drop the curly braces for the API key value, i.e.:

headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': '1111460aa78d4b27****************',
}

Upvotes: 1

Related Questions