Azure translator script was working now getting request not authorized because credentials are missing or invalid
steve corman
15
Reputation points
I had a Translator script working. Suddenly the API started giving response
{ "error": {
"code": 401001,
"message": "The request is not authorized because credentials are missing or invalid."
}
}
Here is the relevant code:
key = "****"
endpoint = "https://api.cognitive.microsofttranslator.com"
location = "global"
path = '/translate'
constructed_url = endpoint + path
params = {'api-version': '3.0', 'to': 'en'}
headers = {
'Ocp-Apim-Subscription-Key': key,
# location required if you're using a multi-service or regional (not global) resource.
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
update_query = "update pulsar set translated_content = %s where id = %s"
select_query = "SELECT id,dewindowfy_content,cld3_lang from bri where dewindowfy_content is not null and cld3_lang ~ 'id|ms|fil' and dup_of_pulsar_unique_id is null and translated_content is null order by random() limit 5000"
@sleep_and_retry
@limits(calls=1, period=61)
def translate(texts,dbids,chars_translated,start):
request = requests.post(constructed_url, params=params, headers=headers, json=texts)
response = request.json()
try:
for item in response:
text = item["translations"][0]["text"]
dbid = dbids.pop(0)
cur.execute(update_query,(text,dbid))
conn.commit()
return
except:
print(json.dumps(response, indent=2))
print('\nchars: {}'.format(chars_translated))
end = time.time()
dur = end - start
print('secs: {}'.format(dur))
rate = chars_translated * 3600 / dur
print('chars/hr: {} \n'.format(rate))
sys.exit()
I found some other threads on this error, which identified the problem as a missing Ocp-Apim-Subscription-Region parameter, but as you can see I have that and my Translator resource "essnentials" section confirms that my location is global. I tried regenerating the key, no help. What to do?
Sign in to answer