Fix ClientAuthenticationError: (401) 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. Code: 401 Message:

Sajal Soni 5 Reputation points
2023-10-20T11:21:05.2466667+00:00

On running below code facing error of
ClientAuthenticationError: (401) 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. 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.

def custom_form_recognizer(key, model_id,blob_container_url,file_names):
    document_analysis_client = DocumentAnalysisClient(endpoint=endpoint, credential=AzureKeyCredential(key))

    document_data = []
    for idx, file_name in enumerate(file_names):
        form_url = blob_container_url + file_name
        poller = document_analysis_client.begin_analyze_document_from_url(model_id, form_url)
        result = poller.result()

Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
1,505 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Ramr-msft 17,651 Reputation points
    2023-10-20T13:32:52.11+00:00

    @Sajal Soni Thanks for the question. The error message you’re seeing typically indicates that the subscription key you’re using for the Azure Document intelligence service is either incorrect or expired. It could also mean that the endpoint you’re using doesn’t match the one associated with your key. Here are a few things you can check:

    1. Subscription Key: Make sure that the key you’re passing to AzureKeyCredential(key) is correct. You can find this in your Azure portal under the ‘Keys and Endpoint’ section of your Form Recognizer resource.
    2. Endpoint: The endpoint should also match the one found in the same ‘Keys and Endpoint’ section in Azure portal.

    Here is the document for authentication. https://azuresdkdocs.blob.core.windows.net/$web/dotnet/Azure.AI.FormRecognizer/4.1.0/index.html#authenticate-the-client