I been trying to solve this error but I can't find what seems to be wrong.
I am using Microsoft Cognitive Services Face API
with python
. Here is my code:
import requests
import json
import http.client, urllib, base64, json
body = {"URL": "http://www.scientificamerican.com/sciam/cache/file/35391452-5457-431A-A75B859471FAB0B3.jsdfpg" }
headers = {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": "xxx"
}
try:
r=requests.post('https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender',json.dumps(body) , headers)
print(r.content)
except Exception as e:
print(format(e))
When I run the script I get:
"code":"Unspecified","message":"Access denied due to invalid subscription key. Make sure you are subscribed to an API you are trying to call and provide the right key."
The thing is that when I put the exact same Key on the console everything works fine. So I am pretty sure it is not the key.
The error must be on my code, but I can't find it.
Any tip in the right direction will be appreciated, Thanks
The error is in the way you conform the request.post call. Parameters to this function are positional, as mentioned in this other post hence the headers are not passed as headers, so the key is not recognized. If you specify what each parameter is, you will avoid this error. That is:
r=requests.post('https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender',params=None, data = json.dumps(body), headers = headers)
Also, the URL to your image does not point to a valid JPEG file (the extension is garbled, probably a typo).