How to resolve Error 401 for Azure OpenAI? Curl commands and sending requests via API returns 401 error

Cybr Edge 20 Reputation points
2024-12-13T07:51:34.19+00:00

I'm encountering issues with both Azure CLI curl commands and the API endpoint for my app when trying to interact with an Azure OpenAI (AOAI) deployment.

I'm attempting to run the following curl command from the Azure CLI to test my OpenAI deployment, but I keep getting "401 Unauthorized" errors. Here’s the command I’m using:

curl -X POST "https://<resource-name>.openai.azure.com/openai/deployments/<deployment-name>/chat/completions?api-version=2024-08-01-preview" \
-H "Ocp-Apim-Subscription-Key: <my-subscription-key>" \
-H "Content-Type: application/json" \
-d '{
    "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello, can you tell me a joke?"}
    ],
    "max_tokens": 50,
    "temperature": 0.7
}'

Despite double-checking the resource name, deployment name, API version, and Ocp-Apim-Subscription-Key, I continue to receive errors.

  • 401 Unauthorized — Despite using the correct API key and ensuring role assignments are in place.

The reason this came about is because I have an application using a similar logic to interact with the Azure OpenAI endpoint. I figured to try the curl commands in Azure CLI to see if the problem is with the app or the AOAI deployment. The app is also failing with the same error:

Status Code: 401  
Response: {"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}

Here’s a snippet of my API call logic within the app:

headers = {
    "Ocp-Apim-Subscription-Key": self.api_key,
    "Content-Type": "application/json"
}
payload = {
    "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello, can you tell me a joke?"}
    ],
    "max_tokens": 50,
    "temperature": 0.7
}
response = requests.post(self.models_url, headers=headers, json=payload)

Here are the logs when I run the app:

Connected to Azure LLM at https://<resource-name>.openai.azure.com/ with deployment '<deployment-name>'.

=== DEBUG INFO ===
URL: https://<resource-name>.openai.azure.com/openai/deployments/<deployment-name>/chat/completions?api-version=2024-08-01-preview
Headers: {'Ocp-Apim-Subscription-Key': '<api-key1>', 'Content-Type': 'application/json'}
Payload: {'messages': [{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'Hello, can you tell me a joke?'}], 'max_tokens': 50, 'temperature': 0.7}
==================


=== RESPONSE INFO ===
Status Code: 401
Response Text: {"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}
=====================

Traceback (most recent call last):
  ...
    raise Exception(
Exception: Failed to retrieve LLM info: 401 Unauthorized - Please check your API key or role permissions.

I have tried multiple troubleshooting steps but am still not able to resolve this error. What I have tried:

  • Verified the keys from Keys and Endpoints of the AOAI match correctly
  • Verified the deployment exists
  • Verified the endpoint URL and deployment name are being used correctly
  • Verified permissions and roles assigned to the user in IAM (Owner, Cognitive Services Contributor, Cognitive Services OpenAI Contributor)
  • Checked for API key issues, regenerated keys, waited 5 min as suggested in documentation, and retried
  • Attempted to re-deploy the OpenAI deployment, created a new deployment but still receive the same error
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,508 questions
{count} votes

Accepted answer
  1. navba-MSFT 27,185 Reputation points Microsoft Employee
    2024-12-18T08:37:16.9033333+00:00

    @Cybr Edge I'm glad to see you were able to resolve your issue. Thanks for posting your solution so that others experiencing the same thing can easily reference this. Since the Microsoft Q&A community has a policy that the question author cannot accept their own answer, they can only accept answers by others, I'll repost your solution in case you'd like to Accept the answer.

    . Issue:

    You were encountering the below 401 error while calling the Azure OpenAI chat completion endpoint:

    Status Code: 401  
    
    Response: {"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}
    

    .

    Resolution:

    You were using the incorrect header for the API key.

    So, you changed the headers to 'api-key' instead of 'Ocp-Apim-Subscription-Key' and this resolved the issue.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.