Getting the error - { "error": { "code": 401001, "message": "The request is not authorized because credentials are missing or invalid." } } while working on Translator service

Shivang Pandey 10 Reputation points
2024-03-15T09:18:16.7966667+00:00

Hi,

I was following the quick start guide for azure translator using python for code and while running the code I am getting the following error -

{

"error": {

    "code": 401001,

    "message": "The request is not authorized because credentials are missing or invalid."

}

}

Need help to solve this and proceed further.

Azure Translator
Azure Translator
An Azure service to easily conduct machine translation with a simple REST API call.
343 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. romungi-MSFT 42,286 Reputation points Microsoft Employee
    2024-03-15T10:43:37.6333333+00:00

    @Shivang Pandey Which QuickStart are you following in this case? Is it this one for python from the documentation.

    This error indicates that you are passing the incorrect subscription key or key of your translator resource. Here is a screen shot of the response if you pass the correct details.

    User's image

    The endpoint in the above request should be

    key = "your_key"
    endpoint = "https://api.cognitive.microsofttranslator.com"
    

    The key is the key available on your translator resource that is available from the keys and endpoints page from azure portal.

    If you are using a multi service cognitive resource, you should pass the location header else it will not be required with the above endpoint. I hope this helps!!

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

  2. Shivang Pandey 10 Reputation points
    2024-03-15T12:50:41.6266667+00:00

    Hi @romungi-MSFT ,

    Attaching the code I used for the Translator even after passing the correct key and location I am getting the same error -

    "error": {

        "code": 401001,
    
        "message": "The request is not authorized because credentials are missing or invalid."
    
    }
    

    }

    Wi

    import requests, uuid, json
    
    # Add your key and endpoint
    key = "xxxxx"
    endpoint = "https://api.cognitive.microsofttranslator.com"
    
    # location, also known as region.
    # required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.
    location = "westus"
    
    path = '/translate'
    constructed_url = endpoint + path
    
    params = {
        'api-version': '3.0',
        'from': 'en',
        'to': ['fr', 'zu']
    }
    
    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())
    }
    
    # You can pass more than one object in body.
    body = [{
        'text': 'I would really like to drive your car around the block a few times!'
    }]
    
    request = requests.post(constructed_url, params=params, headers=headers, json=body)
    response = request.json()
    
    print(json.dumps(response, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': ')))