how to call document intelligence by accesstoken

Wu, Stanko-SD 0 Reputation points
2024-01-16T03:54:47.1633333+00:00

User's image

User's image

I get the sp accesstoken, but can't call the document intelligence api, why?
I'm sure the sp have a role in document intelligence.
I only want to use sp to call the api, so needn't show me another way, thanks.

Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
2,115 questions
{count} votes

1 answer

Sort by: Most helpful
  1. dupammi 8,615 Reputation points Microsoft External Staff
    2024-01-16T10:20:56.2066667+00:00

    Hi @Wu, Stanko-SD ,

    Thank you for using the Microsoft Q&A. To call the Document Intelligence API using a service principal, you need to obtain an access token using your SP credentials and then use it to make a request to the API endpoint. User's image

    You can obtain the access token by setting up a POST request to the Azure AD token endpoint in Postman with above highlighted details in yellow and then use it to make a POST request to the Document Intelligence API endpoint.

    Step1: Obtain an Access Token

    Open Postman.

    Set up a POST request to the Azure AD token endpoint:

    Request URL: https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token

    Replace {tenant_id} with your Directory (tenant) ID.

    In the Headers section, add the following key-value pairs:

    Content-Type: application/x-www-form-urlencoded

    In the Body section,

    select x-www-form-urlencoded and add the following key-value pairs:

    grant_type: client_credentials

    client_id: {your_client_id}

    client_secret: {your_client_secret}

    scope: https://graph.microsoft.com/.default

    Replace {your_client_id} and {your_client_secret} with your SP's Application (Client) ID and Client Secret.

    Click "Send" to obtain the access token and take a note of this to use in Step2.

    Please find below Postman request (Post) for Step1 at my side for your reference: User's image

    Step2 : Use the Access Token in the Document Intelligence API Request Now, you have the access token. Use it to make a request to the Document Intelligence API. Set up a POST request in Postman to the Document Intelligence API endpoint: Request URL: {document intelligence endpoint}/formrecognizer/documentModels/prebuilt-read:analyze?api-version=2023-07-31 In the Headers section, add the following key-value pairs: Content-Type: application/json Accept: application/json Authorization: Bearer {your_access_token} Replace {your_access_token} with the actual access token obtained in Step 1. In the Body section, add your payload: {"base64Source": "your base64-encoded image data"} Replace "your base64-encoded image data" with the actual base64-encoded content of your image. Click "Send" to make the API request. Remember to replace placeholder values with your actual SP credentials, access token, and image data. Below is a simplified example request that I reproduced at my end for Step2 for your reference. Depending on your actual setup, you may need to adjust headers, parameters, or body content. User's image

    From above Step2, copy the "Operation-Location" URL Value and make another "GET" request with the required key to check the status of your document processing. Note: Kindly recheck if the SP or the application needs a role to be assigned to get access. for the "Cognitive Services User". This could be the reason for the "Unauthorized" error appearing. Please also try adding a role and checking your request again by giving the correct scope. For more details, please refer this Assign a role to a service principal Below is step3 for your reference:

    Step3:
    User's image


    Apart from these, please also check if the access token has been "copied correctly", "is VALID" and has the "required permissions" to call the Document Intelligence API. For more details, please refer Rest API Document Models - Analyze Document .

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    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.