API Sharepoint : List and download files with App Azure

155 Views Asked by At

I need to connect to a Sharepoint with API, download list of file from API and then download it.

I've tried to create App in Microsoft Entra and give it all rights with "sites.fullcontrol.all" But it doesn't work.

When I use GetFolderByServerRelativePath() I get this response :

{"error_description":"Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown."}

I am not sure which endpoint I've to use.

Can someone say me which endpoint use please ?

1

There are 1 best solutions below

0
On BEST ANSWER

The error AudienceUriValidationFailedException usually occurs if you are generating token with Microsoft Graph scope and using it for SharePoint API.

Initially, I too got same error when I used GetFolderByServerRelativePath() with token generated with MS Graph scope:

GET http://xxxxxxxxx.sharepoint.com/sites/site_name/_api/web/GetFolderByServerRelativePath(decodedUrl='Shared Documents/Test')

Response:

enter image description here

To resolve the error, you need to generate access token with either SharePoint scope(https://yourtenant.sharepoint.com/.default) or switch to Graph API calls.

In my case, I registered one Azure AD application and granted API permissions of Application type as below:

enter image description here

Now, I generated access token using client credentials flow via Postman like this:

POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token
grant_type:client_credentials
client_id: appId
client_secret: secret 
scope: https://graph.microsoft.com/.default

Response:

enter image description here

You can use this token to call below Graph API query that list files along with download URL:

GET https://graph.microsoft.com/v1.0/sites/<siteID>/drives/<doclib driveID>/root/children/

Response:

enter image description here

Now, click on that download URLs from response where files will be downloaded successfully like this:

enter image description here

Reference: azure - Download File from Secure Sharepoint using Rest API - AudienceUriValidationFailedException - Stack Overflow