0

I am unable to download file from SharePoint using Office365-REST-Python-Client.

I am getting then below error: office365.runtime.client_request_exception.ClientRequestException: (None, None, "401 Client Error: Unauthorized for url: https://domain.sharepoint.com/sites/test/_api/Web/getFileByServerRelativePath(DecodedUrl='%2Fsites%2Ftest%2FShared%20Documents%2FSample_Compliance_content.pdf')?$select=ServerRelativePath,Id")

I have went through multiple documents to download the SharePoint file but none of them worked out. Please help me with this error.

So far I have done following steps:

  1. I signed up for free Developer 365 program.
  2. Created a site in SharePoint and uploaded a document there.
  3. I want to download that document using Office365-REST-Python-Client.
  4. For authorization I did all the steps mentioned in the Office365-REST-Python-Client document.

I followed below steps for authentication as mentioned in the Office365-REST-Python-Client.

  1. Login to the home page of the Azure Portal
  2. Navigate to "Azure Active Directory" using the three bars in the top right corner of the portal
  3. Select "App registrations" in the navigation panel on the left
  4. Search for and select your relevant application
  5. In the application's "Overview" page, the client id can be found under "Application (client) id"
  6. In the application's "Certificates & Secrets" page, the client secret can be found under the "Value" of the "Client Secrets." If there is no client secret yet, create one here.

Below is my code snippet

  1. I am using ClientCredential to use client id and client secret (not secret id) and ClientContext
  2. I want to download the pdf file Sample_Compliance_content.pdf in the SharePoint site
  3. But it is giving me the above mentioned error.

enter image description here

1 Answer 1

0

For some reason clientID ClientSecret auth is having issues.

Please use certificate auth flow. https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread

  1. Generate a Private Key: Use OpenSSL to create a private key. The command might look like
openssl genrsa -out privatekey.pem 2048
  1. Create a Certificate Signing Request (CSR): Generate a CSR using your private key with a command like
openssl req -new -key privatekey.pem -out csr.pem
  1. Generate a Self-Signed Certificate: Use the CSR to create a self-signed certificate, with a command like
openssl x509 -req -days 365 -in csr.pem -signkey privatekey.pem -out certificate.pem

Upload the certificate.pem to azure and use the privateKey.pem to authenticate in the client

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.