I'm trying to follow documentation and read files from SharePoint using APIs. I have a client_id+client_secret and I'm able to retrieve an access token like so:
I then try to use this token and calling GetFolderByServerRelativeUrl
:
I get a 401 (Unauthorized) error: ID3035: The request was not valid or is malformed.
I'm not entirely familiar with SharePoint so not even sure 'AAA' is considered a folder. It's a document library from which I want to access the content of "Monthly reports" folder.
Any help would be appreciated.
I created one document library named AAA
in Finance SharePoint site with one folder like this:
I registered one Entra ID application and granted API permissions of Delegated type as below:
Now, I generated access token using client credentials flow via Postman as below:
POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token
grant_type:client_credentials
client_id:appID
client_secret:secret
scope: https://tenant.sharepoint.com/.default
Response:
When I used this token to call SharePoint REST API, I too got same error as below:
GET https://tenant.sharepoint.com/sites/Finance1/_api/web/GetFolderByServerRelativeUrl('AAA')
Response:
To resolve this, generate access token using delegated flow like authorization code as we granted permissions of Delegated type in app registration.
Initially, I got the code
value by running below authorization URL in browser:
https://login.microsoftonline.com/tenantID/oauth2/v2.0/authorize
?client_id=appID
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope=https://tenant.sharepoint.com/.default
&state=12345
Now, I used this code to generate access token with authorization code flow via Postman:
POST https://login.microsoftonline.com/tenantID/oauth2/v2.0/token
grant_type:authorization_code
client_id:appID
client_secret:secret
scope: https://tenant.sharepoint.com/.default
code:code
redirect_uri: https://jwt.ms
Response:
When I used this token to call SharePoint REST API, I got the response successfully like this:
GET https://tenant.sharepoint.com/sites/Finance1/_api/web/GetFolderByServerRelativeUrl('AAA')
Response:
To retrieve the files present in that folder, you can use below SharePoint REST API call:
GET https://tenant.sharepoint.com/sites/Finance/_api/web/GetFolderByServerRelativeUrl('AAA/Monthly Reports')/Files
Response:
You can also use Microsoft Graph API to download SharePoint files via REST API.