azuresharepoint

Accessing SharePoint Files via API Results in 'AudienceUriValidationFailedException'


I have used all the correct values in the following code, and I am successfully receiving an access token:

url = "https://login.microsoftonline.com/<tenant-id>/oauth2/token"

payload = {
"client_id": "CLIENT-ID",
"client_secret": "CLIENT-SECRET",
"grant_type": "client_credentials",
"scope": "https://graph.microsoft.com/.default" 
}

I have given it all the possible amount of API permission (for testing purposes ofcourse)

but when I access it through the REST API below: (I have a site called TestSite, and a folder called Guide)

https://{site_url}.sharepoint.com/sites/TestSite/_api/web/GetFolderByServerRelativeUrl('/sites/TestSite/Shared%20Documents/Guide')/Files

I keep getting the error 401 'error_description': "Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown.

Why is it like this? Can anyone please help me with this issue?

I have tried everything there is, but I still can't find a solution to access the SharePoint files from the API


Solution

  • I created one folder named Guide in "TestSite" SharePoint site and uploaded below files in it:

    enter image description here

    To access SharePoint files present in site folder via Graph API, you can make use of below API call:

    GET https://graph.microsoft.com/v1.0/sites/<siteId>/drives/<driveId>/root:/Guide:/children
    

    To get the values of siteId and driveId, you can run below Graph API calls:

    Site ID:

    GET https://graph.microsoft.com/v1.0/sites/root:/sites/TestSite
    

    enter image description here

    Drive ID:

    GET https://graph.microsoft.com/v1.0/sites/siteIdfromAbove/drives?$filter=name eq 'Documents'
    

    enter image description here

    Now, I ran below sample code and got the response successfully with SharePoint files like this:

    import requests
    
    tenant_id = "tenantId"
    client_id = "appId"
    client_secret = "secret"
    scope = "https://graph.microsoft.com/.default"
    
    url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
    
    payload = {
        "client_id": client_id,
        "client_secret": client_secret,
        "grant_type": "client_credentials",
        "scope": scope,
    }
    
    try:
        response = requests.post(url, data=payload)
        response.raise_for_status()
        token_data = response.json()
        access_token = token_data.get("access_token")
    
    
        api_url = "https://graph.microsoft.com/v1.0/sites/siteId/drives/driveId/root:/Guide:/children"
        headers = {
            "Authorization": f"Bearer {access_token}",
            "Content-Type": "application/json",
        }
    
        api_response = requests.get(api_url, headers=headers)
        if api_response.status_code == 200:
            files = api_response.json().get("value", [])
            for file in files:
                print(f"File Name: {file['name']}, ID: {file['id']}")
        else:
            print(f"API Call Failed: {api_response.status_code}, {api_response.text}")
    
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")
    

    Response:

    enter image description here