0

我試圖通過微軟使用Python 3.2微軟情感的視頻API的Python 3.2

我遇到下面的錯誤分析通過情感API視頻:

b'{ "error": { "code": "Unauthorized", "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." } }'

我使用情感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

你的代碼工作。只要確保在生成API密鑰後等待10分鐘,以便它開始工作(它在Azure門戶中就這麼說)。

另外,一般來說,對於Cognitive Services,請確保您的API密鑰對應於您嘗試擊中的區域(美國西部等)