Failed to call Azure OpenAI API: { "statusCode": 401, "message": "Unauthorized. Access token is missing, invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired." }

Ravi Modukuri 15 Reputation points
2024-09-13T12:54:01.1433333+00:00

Failed to call Azure OpenAI API: { "statusCode": 401, "message": "Unauthorized. Access token is missing, invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired." }

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,080 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Daniel Fang 1,060 Reputation points MVP
    2024-09-14T04:13:00.86+00:00

    It could also be a delay for the service to be ready and servicing requests if it is a new resource you just created. Checking all the URLs and API keys and try after 1h. The other thing is that also try the secondary key, there is once that primary key not working for us but secondary key actually worked.

    0 comments No comments

  2. Geena Kim 0 Reputation points
    2024-11-16T02:35:01.95+00:00

    Can someone give an example? I matched the api version the same as the model deployment endpoint's target uri- for example, (Note that the real address is masked with [azureopenai-resourcename] since everyone's case will be different)
    https://[azureopenai-resourcename].openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview

    Then I set the required environment variables like

    export AZURE_OPENAI_API_KEY="<replaced with my key>"

    export AZURE_OPENAI_ENDPOINT="https://[azureopenai-resourcename].openai.azure.com/"

    export OPENAI_API_TYPE="azure"

    export OPENAI_API_VERSION="2024-08-01-preview" ## This is to match the api version that shows up in the target uri.

    But it still gives the error messages like:

    INFO:httpx:HTTP Request: POST https://[azureopenai-resourcename].openai.azure.com//openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview "HTTP/1.1 401 Unauthorized"

    Error code: 401 - {'statusCode': 401, 'message': 'Unauthorized. Access token is missing, invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired.'}

    When I use LangChain to load embedding model, it works ok, but while using reranker using gpt model(s) it makes a problem.
    I tested with export OPENAI_API_TYPE="openai" and used my personal account (in this case it didn't need any other auth other than OPENAI_API_KEY.
    So it seems the langchain's rerank works and somehow it breaks when azure credentials are passed.

    So other than those envs (AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, OPENAI_API_TYPE, OPENAI_API_VERSION), what "Access token" is required??
    And what is https://cognitiveservices.azure.com??
    Do we need to set up another endpoint other than the model deployment (as above uri shows)?


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.