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.
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:
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.
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:
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.