Hi Ahmed Badawi
Thanks for reaching out to Microsoft Q&A forum support.
Based on your description, I understand you're encountering a 401 Unauthorized error when trying to access an Excel file hosted on a SharePoint site using the Office365-REST-Python-Client library in Python. This occurs despite having set up an Azure AD app registration with the Sites.Selected permission and configured client certificates.
As a forum moderator, I may not be able to provide a definitive solution due to limitations in available resources and access to your specific environment. However, I’ve conducted some research on this issue.
This problem has been raised by other users across various technical communities such as:
Github: 401 Client Error: Unauthorized · Issue #314 · vgrem/office365-rest-python-client
And it seems that the most feasible solution to this problem is:
Step 1: Install cmdlet if you don't have it
Install-Module -Name Microsoft.Online.SharePoint.PowerShell
Step 2: Connect and run the command
Connect-SPOService -Url https://[tenant]-admin.sharepoint.com -Credential ******@domain.com
Set-SPOTenant -DisableCustomAppAuthentication $false
Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link
That said, you can try the approach I suggested above to see if the problem persists and you can read more insight from given links, but please note that this is an unofficial approach from Microsoft.
Hope you will solve your problem soon
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.