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 15 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.
427 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. steve corman 15 Reputation points
    2024-06-13T23:17:14.5566667+00:00

    This just happened to me. After 45 min of reading posts like this, regenerating keys, double-checking code, etc., I found the problem had nothing to do with missing or invalid credentials. I had not yet switched my plan over to pay-as-you-go and I had used up my free quota.

    Maybe some of you MS people could pass along to the API team that they need to revise the error message to account for this possibility.

    3 people found this answer helpful.

  2. romungi-MSFT 47,526 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

  3. Shivang Pandey 15 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=(',', ': ')))
    

  4. Mirek Kędzierski 0 Reputation points
    2024-11-15T09:57:18.1266667+00:00

    I have the same problem. Even when testing translator here
    User's image

    I have response
    error: {code: 401001, message: "The request is not authorized because credentials are missing or invalid."}

    1. code: 401001
    2. message: "The request is not authorized because credentials are missing or invalid."
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.