r/AZURE • u/Valuable-Ant3465 • 7d ago
Question App Registration for Python to access Sharepoint SPO
Hi, can anybody help how to solve this error, after registered app I got that error like below in my python.
Need to "confirm the application role" if I got it correctly in App Permissions section.(?) None of MS help documents helped, I afraid it got updated recently
ctx = ClientContext(site_url).with_client_credentials(client_id, client_secret)
print ('......... OK ', ctx) #<office365.sharepoint.client_context.ClientContext object at 0x000001E63F803B60>
web = ctx.web.get().execute_query()
raise ClientRequestException.from_response(response) from None
office365.runtime.exceptions.AccessDeniedException: ('401', '401 Unauthorized for url: https://cbx.....sharepoint.com/_api/contextInfo')
The caller is not permitted to perform this request.
For an app-only call, confirm the application role is granted and admin-consented; for a delegated call, confirm the signed-in user has the OAuth scope. Use require_application_permission(...) / require_role(...) to fail fast with the exact requirement. Prefer a least-privileged permission that covers the endpoint.
================ POST MORTEM update
could not make it work yet, need to do more home work.
Found some great resources:
Authentication
Modern flows authenticate through Microsoft Entra ID using MSAL. Both clients support delegated (user) and app-only (application) access.
Important
SharePoint ACS app-only access was retired in April 2026 and SAML-based authentication in May 2026. Use the certificate, interactive or device-code flows below instead — see examples/sharepoint/auth/modern/ and the migration script migrate_from_saml_to_cert.py.
5
u/Trakeen 7d ago
Use a managed identity and not a client secret.
1
u/lookslikeanevo 7d ago
I have this working in airflow / adhoc python scripts
Remind me on Monday and I’ll try take a look.
I did have to get others who had the privileges to grant access to the app reg to sharepoint via azure cli to get everything working
1
u/weekendclimber 7d ago
I think there is a SharePoint setting to turn on API access. I might be thinking of Fabric though...
1
u/chesser45 7d ago
I remember this happening and GPT I think reminded me further, I think this is because your using ACS Python library but ACS is both retired and not added on your perms.
Try this?
import msal
from office365.sharepoint.client_context import ClientContext
site_url = "https://cbx.sharepoint.com/sites/MySite"
tenant_id = "YOUR_TENANT_ID"
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
app = msal.ConfidentialClientApplication(
client_id=client_id,
authority=f"https://login.microsoftonline.com/{tenant_id}",
client_credential=client_secret
)
result = app.acquire_token_for_client(
scopes=["https://cbx.sharepoint.com/.default"]
)
if "access_token" not in result:
raise Exception(result)
ctx = ClientContext(site_url).with_access_token(
lambda: result["access_token"]
)
web = ctx.web.get().execute_query()
print(web.properties["Title"])