Download PDF files from Sharepoint programmatically in Python

Hamed Fathi 0 Reputation points
2024-01-30T08:55:15.52+00:00

I am trying to connect to Sharepoint and get all PDF files in the Sharepoint directory

    cert_thumbprint = "43EA...2910F3D106F0954478"
    private_key = """-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDO5sFhHefRsI+m
O6tBWs0OnNRr44RH1ljbjByK2KQvbKI+KFgwyeyogt3065lY6W4Xpw3IxqZZiSmE
...
KerXXf8mIadaCe3rLmQGernc6O4ZP+sbRrw6zGFsw/zd2Zmuzov62DbAAQKBgQCs
...
c/dppy+PQQYQ/29WMt5/ITTI0lEHRqkZdNBLJcWX6iJ2Lh+QcTBeIzv0RzxCfZCl
bmRwnYv0Rp2CILKY28sn54ivMxMEorSaO0NhlOM5NCQUuYNwt2+JiIdjccmrNJs0
O8wT9fwTf9V1xV6m7pQ9EHv4DA==
-----END PRIVATE KEY-----"""

    cert = {
        "private_key": private_key,
        "thumbprint": cert_thumbprint
    }

    import msal
    import requests

    app = msal.ConfidentialClientApplication(
        "1257aa45-...-...-...-56fae68aebfe",
        authority=f"https://login.microsoftonline.com/91b358ee-...-...-...-0a2b9d5940b8",
        client_credential=cert
    )
    token_response = app.acquire_token_for_client(
        scopes=["https://mygmbh.sharepoint.com/.default"])
    access_token = token_response.get('access_token')
    headers = {
        'Authorization': 'Bearer ' + access_token,
        'Accept': 'application/json;odata=verbose',
        'Content-Type': 'application/json'
    }

    sharepoint_url = "https://mygmbh.sharepoint.com/sites/QMS/_api/web/GetFolderByServerRelativeUrl('Freigegebene Dokumente')/Files"
    response = requests.get(url=sharepoint_url, headers=headers)
    files = response.json()['d']['results']
    pdf_files = [file for file in files if file['Name'].endswith('.pdf')]

Until this step, I can see the list of PDF files User's image

But for the below, I got an error

    for file in pdf_files:
        download_url = file['ServerRelativeUrl']
        token_response2 = app.acquire_token_for_client(
            scopes=["https://mygmbh.sharepoint.com/.default"])
        access_token2 = token_response2.get('access_token')
        headers2 = {
            'Authorization': 'Bearer ' + access_token2,
            'Accept': 'application/json;odata=verbose',
            'Content-Type': 'application/json'
        }
        dl = "https://mygmbh.sharepoint.com" + download_url
        response2 = requests.get(dl, headers=headers2, stream=True) # <--- ERROR

User's image

If you want to know my permissions User's image

Can anyone help me with this? I want to be able to download PDF files without permission issues.

Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.