0

我一直在尝试解决此错误,但找不到似乎有问题的地方。

我正在Microsoft Cognitive Services Face API使用python. 这是我的代码:

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))

当我运行脚本时,我得到:

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

问题是,当我将完全相同的密钥放在控制台上时,一切正常。所以我很确定这不是关键。

错误一定在我的代码上,但我找不到。

任何正确方向的提示将不胜感激,谢谢

4

1 回答 1

3

错误在于您符合request.post调用的方式。此函数的参数是位置参数,正如其他帖子中所述,因此标头不会作为标头传递,因此无法识别密钥。如果您指定每个参数是什么,您将避免此错误。那是:

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)   

此外,图像的 URL 未指向有效的 JPEG 文件(扩展名是乱码,可能是拼写错误)。

于 2016-05-02T03:50:33.737 回答