Azure openai Error: 401"Unauthorized. Access token is missing, invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired."

nam seungwoo 0 Reputation points
2024-11-23T20:48:54.56+00:00

Hi all,

In my simple python code below, it happens run time error like below,

Error: 401 { "statusCode": 401, "message": "Unauthorized. Access token is missing,

invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired." }

import requests

# Azure OpenAI Configuration
API_KEY = "xxxxxx"  # API key generated in Azure OpenAI
ENDPOINT = "https://xxxxx.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview" #Endpoint is copied from openAI

payload = {
    "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Tell me a joke."}
    ],
    "temperature": 0.7,
    "max_tokens": 100
}

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {API_KEY}"
}

# API call
response = requests.post(ENDPOINT, headers=headers, json=payload)

# print result
if response.status_code == 200:
    print("Response:", response.json())
else:
    print("Error:", response.status_code, response.text)


API key & End Point is copied from Azure OpenAI resource below,

User's image and both key info

User's image

and the following model is created in SwedenCentral as Azure AI services,User's image

and exists like below

User's image

I used copy/paste the each key and other necessary values, so it will be no problem.

Or my free trial subscription is the problem? Upgrade is needed?User's image

Thanks!

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,321 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,938 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Max Lacy 180 Reputation points
    2024-11-24T22:10:32.38+00:00

    Hello @nam seungwoo ,

    Looks like you're trying to response from a chat completion 4o model. When calling a chat completion you can use:

    1. API Key Authentication: You include the API key in the api-key HTTP header for each request.
    2. Microsoft Entra ID Authentication: You use a Microsoft Entra token, included in the Authorization header as Bearer YOUR_AUTH_TOKEN.

    In the code below you're combining the two. If you want to use the API key in your screenshot switch your header to "api-key":API_KEY. If you want to use Microsoft Entra ID Authentication you'll need to do some additional configuration.

    Below is from a working chat.completions request:

    import os 
    import requests
    import base64 
    # Configuration 
    API_KEY = "YOUR_API_KEY" 
    IMAGE_PATH = "YOUR_IMAGE_PATH" 
    encoded_image = base64.b64encode(open(IMAGE_PATH, 'rb').read()).decode('ascii')
    
    
    headers = { "Content-Type": "application/json", "api-key": API_KEY, } 
    
    # Payload for the request 
    payload = { "messages": [ { "role": "system", "content": [ { "type": "text", "text": "You are an AI assistant that helps people find information." } ] } ], "temperature": 0.7, "top_p": 0.95, "max_tokens": 800 } 
    ENDPOINT = "https://aiestimator.openai.azure.com/openai/deployments/AIEstimator/chat/completions?api-version=2024-02-15-preview" 
    # Send request 
    try: response = requests.post(ENDPOINT, headers=headers, json=payload) 
    
    

    ***To ensure the security of your applications and data, never share your API keys with anyone.

    0 comments No comments

  2. navba-MSFT 25,765 Reputation points Microsoft Employee
    2024-11-25T07:06:27.96+00:00

    @nam seungwoo Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    .

    The issue is in the below line of your code:

    "Authorization": f"Bearer {API_KEY}"

    Please replace the above line with my below code:

    "api-key": API_KEY

    .

    This should resolve your issue. If you have any follow-up questions, please let me know. I would be happy to help.

    **

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    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.