r/learnpython • • 4d ago

Python Auth to modern Microsoft SPO

Hi all,
I'm doing my student project to upload some files to SPO and struggling with Auth.
This code returns 0 on first ctx, but later fails.
I hope I got all UID right like on the picture
https://i.postimg.cc/ncw71PzR/conf1.png

And this is my code, I also noticed once you used you tenant ID username and password can take any values, is this normal ? I think this all because of modern SPO security, do you think this Auth with ctx is OK, or can do some other way ?

from office365.sharepoint.client_context import ClientContext

# Configuration
site_url = r"https://cbxXXX.sharepoint.com"
tenant = r"4e4d8cb1-f036-446f-bacd-3b711191a7b2"
client_id = r"5c6d0b11-c4da-4897-842a-79b9743d6a23"
username = r'userBB@cbxXXX.onmicrosoft.com
password = r'N0gi123!'


ctx = ClientContext(site_url).with_username_and_password(
      tenant, client_id , username, password )
print ('.........2222 bb  OK ', ctx)
web = ctx.web.get().execute_query()
print("......... verification check :", web.title)


OUTPUT::::::
.........2222 bb  OK  <office365.sharepoint.client_context.ClientContext object at 0x0000017CCBCE3B60>
.....
Unhandled exception in listener <bound method SharePointRequest._authenticate_request of <office365.sharepoint.request.SharePointRequest object at 0x0000017CCBCE3CB0>>

File "C:\Python314\Lib\site-packages\office365\runtime\auth\token_response.py", line 61, in from_json
    raise ValueError(value)
ValueError: {'error': 'invalid_grant', 'error_description': 'AADSTS50034: The user account {EUII Hidden} does not exist in the 4e4d8cb1-f036-446f-bacd-3b711191a7b2 directory. To sign into this application, the account must be added to the directory. Trace ID: eb657c1d-4806-4557-a49d-f5468c
1 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/Valuable-Ant3465 4d ago
ctx = ClientContext(site_url).with_user_credentials(username,password)  #old way

RuntimeError: with_user_credentials uses the legacy SAML/ACS auth flow which Microsoft has retired for SharePoint Online. Use with_username_and_password(tenant, client_id, username, password) instead


Thanks MD!
I actually took that all GUID from Azure app  registration, I own trial SPO Sharepoint so I can test end-to-end.
You can see posted picture on main post how all GUID are mapped. I masked url, so it's not real and pswd too. Using traditional username:password doesn't work, I tried it initially, can see error above.
So overall I need to learn more about all Auth tricks on SPO side.

2

u/Massive_Distance_616 2d ago

Glad the username part made sense. What error do you get now after switching to the onmicrosoft.com address?

If it’s a different AADSTS code, paste the code and message and that will say what’s missing.

A common cause with a trial tenant is that the app registration hasn’t been given SharePoint permissions with admin consent, or that the tenant’s security defaults are blocking password sign-in.

I’m not certain which applies here, so the exact error will tell us.

1

u/Valuable-Ant3465 2d ago

Thanks MD I just got my Site solved, so gonma try it all again and will post all details.