Hi @G, Bhagyalaxmi ,
Since the code is about python. It's out of our support scope. So we are unable to test the code for you. In this case, 401 Unauthorized
often caused by the client id do not have enough permission.
Please refer to the following document to register the app only
https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
The scope of the App's Permission Request XML should refer to the following document
https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/add-in-permissions-in-sharepoint
Then you can generating Bearer token using client id, secret and run the code again
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.