azuresharepointmicrosoft-graph-apisharepoint-api

Trying to Authenticate with SharePoint and get list of files via API using access token


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:

enter image description here

I then try to use this token and calling GetFolderByServerRelativeUrl:

enter image description here

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.

enter image description here

Any help would be appreciated.


Solution

  • I created one document library named AAA in Finance SharePoint site with one folder like this:

    enter image description here

    I registered one Entra ID application and granted API permissions of Delegated type as below:

    enter image description here

    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:

    enter image description here

    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:

    enter image description here

    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
    

    enter image description here

    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:

    enter image description here

    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:

    enter image description here

    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:

    enter image description here

    You can also use Microsoft Graph API to download SharePoint files via REST API.

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