最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Azure openai error 401, "Unauthorized. Access token is missing, invalid, audience is incorrect (https:cognitiv

programmeradmin2浏览0评论

Here is my python code, and encountered run time error,

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

invalid, audience is incorrect (), or have expired." }

import requests

# Azure OpenAI Configuration
API_KEY = "xxxxxx"  # API key from Azure OpenAI
ENDPOINT = ";

# API data
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)

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

Apikey&ENDPOINT

and gpt-4o model in swedencentral as resource type of Open AI service


model

I rechecked key and other values and topics related this issue. I can't find anything to solve it.

There will be no problem copying and pasting the key values ​​and other necessary values. API version seem correct.

or, I am using free trial, so need to upgrade subscription? I don't think so.

Here is my python code, and encountered run time error,

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 from Azure OpenAI
ENDPOINT = "https://xxx.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview"

# API data
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)

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

Apikey&ENDPOINT

and gpt-4o model in swedencentral as resource type of Open AI service


model

I rechecked key and other values and topics related this issue. I can't find anything to solve it.

There will be no problem copying and pasting the key values ​​and other necessary values. API version seem correct.

or, I am using free trial, so need to upgrade subscription? I don't think so.

Share Improve this question asked Nov 24, 2024 at 3:25 Doobie NamDoobie Nam 111 silver badge1 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 2

You use bearer token authorization when you are acquiring the token using Azure AD. Since you are using the API key, you need not do this.

Please change the following code:

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

to

headers = {
    "Content-Type": "application/json",
    "api-key": f"{API_KEY}"
}

And you should not get this error.

I fixed it creating another deployment. Thanks!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论