我正在尝试使用Python 3.2通过Emotion API分析视频
我遇到以下错误:
b'{“error”:{“code”:“Unauthorized”,“message”:“由于订阅密钥无效而拒绝访问。请确保您订阅了您尝试呼叫的API并提供了正确的密钥“。 '}
我正在使用Emotion API订阅密钥(我还使用了Face API密钥和计算机视觉密钥以防万一)。
代码:
import http.client, urllib.request, urllib.parse, urllib.error, base64
headers = {
# Request headers
'Ocp-Apim-Subscription-Key': '{subscription key}',
}
params = urllib.parse.urlencode({
})
try:
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("GET", "/emotion/v1.0/operations/{oid}?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
答案 0 :(得分:0)
您的代码有效。只需确保在生成API密钥后等待10分钟,以便它开始工作(在Azure门户中这样说)。
此外,通常对于认知服务,请确保您拥有的API密钥对应于您尝试访问的区域(美国西部等)