I am using Python 3.8.3 and try to use Azure Translation. Based on the example of (https://docs.microsoft.com/nl-nl/azure/cognitive-services/Translator/quickstart-translate?pivots=programming-language-python) I try to re-create the example for myself and minimized the code.
I create an Azure Resource (Translate) and copied the key and the endpoint in the code. But when I run the code, I get the following error:
"code": 401000,
"message": "The request is not authorized because credentials are missing or invalid."
Can someone please explain what i doing wrong and how to fix this issue!
I use this code:
import os, requests, uuid, json
path = '/translate?api-version=3.0'
params = '&to=de&to=it'
constructed_url = "https://api.cognitive.microsofttranslator.com" + path + params
headers = {
'Ocp-Apim-Subscription-Key': 'xxxxxxxxxxxx',
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
# You can pass more than one object in body.
body = [{
'text' : 'Hello World!'
}]
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()
print(json.dumps(response, sort_keys=True, indent=4, separators=(',', ': ')))
Many thanks Erik
//
in the url between the base-URL andpath
? – Hampus Larsson"https://api.cognitive.microsofttranslator.com" + path + params
It's a small change, but it would drastically change the path to the API. – Hampus LarssonIf you are using a Cognitive Services multi-service subscription, you must also include the Ocp-Apim-Subscription-Region in your request parameters
. If that's what you're using, then you might have to provide that in theheaders
variable as well. – Hampus Larsson