Hi @Alexander Kruk
Thank you for posting your question in the Microsoft Q&A forum.
If you're encountering error ID3035: The request was not valid or is malformed while connecting to SharePoint Online via REST API using the client credentials flow, it typically points to issues with the resource scope or how the access token is being used.
Here are some troubleshooting steps you may try:
1.When using client credentials flow for SharePoint Online, the scope should be https://{tenant}.sharepoint.com/.default. Do not use the site URL or domain directly.
payload = {
'client_id': client_id,
'client_secret': client_secret,
'grant_type': 'client_credentials',
'scope': 'https://{tenant}.sharepoint.com/.default'
}
2.Ensure your app registration has the necessary application permissions for SharePoint:
Go to Entra ID > App registrations > Your app > API permissions > Add SharePoint > Application permissions > Sites.Read.All or Sites.FullControl.All > Click Grant admin consent.
Without this, the token won’t have valid permissions for SharePoint.
3.Your web_url should target the full site collection path, not just the domain.
For example:
web_url = f"https://{tenant}.sharepoint.com/sites/{site_name}/_api/web"
4.When making a GET request to the SharePoint REST API, you should remove data=payload. Only the access token needs to be included in the request headers:
headers = {
'Accept': 'application/json;odata=verbose',
'Authorization': f"Bearer {access_token}"
}
response = requests.get(web_url, headers=headers)
I hope this helps.
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.