Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

AADSTS90023: Invalid STS request (Python and Office365 SharePoint)

Open poroc300 opened this issue 1 year ago • 4 comments

I am trying to access files on my organisation's SharePoint through Python. I have installed office365-rest-python-client (v.2.5.5) and I am using Python 3.11.5 and Ubuntu 22.04.1 LTS. I have tried to run this code, with multiple accounts, but I always get the same error. Note that for example my password does not contain any special characters.

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

url = "https://{netorg_number}.sharepoint.com/sites/{company_name}/SitePages/Home.aspx"
user = "[email protected]"
password = "password"

ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(user, password):
    ctx = ClientContext(url, ctx_auth)
    web = ctx.web
    ctx.load(web)
    ctx.execute_query()
    print("Authentication successful")

I get the following error:

An error occurred while retrieving token from XML response: AADSTS90023: Invalid STS request.
Traceback (most recent call last):

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File ~/Desktop/repos/my_repo/sharepoint.py:44
    ctx.execute_query()

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/client_runtime_context.py:185 in execute_query
    self.pending_request().execute_query(qry)

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/client_request.py:37 in execute_query
    response = self.execute_request_direct(request)

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/client_request.py:47 in execute_request_direct
    self.beforeExecute.notify(request)

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/types/event_handler.py:41 in notify
    listener(*args, **kwargs)

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/sharepoint/client_context.py:277 in _authenticate_request
    self.authentication_context.authenticate_request(request)

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/auth/authentication_context.py:239 in authenticate_request
    self._authenticate(request)

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/auth/authentication_context.py:212 in _authenticate
    provider.authenticate_request(request)

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/auth/providers/saml_token_provider.py:85 in authenticate_request
    self.ensure_authentication_cookie()

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/auth/providers/saml_token_provider.py:97 in ensure_authentication_cookie
    self._cached_auth_cookies = self.get_authentication_cookie()

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/auth/providers/saml_token_provider.py:109 in get_authentication_cookie
    token = self._acquire_service_token_from_adfs(user_realm.STSAuthUrl)

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/auth/providers/saml_token_provider.py:180 in _acquire_service_token_from_adfs
    token = self._process_service_token_response(response)

  File ~/anaconda3/envs/dscience/lib/python3.11/site-packages/office365/runtime/auth/providers/saml_token_provider.py:247 in _process_service_token_response
    raise ValueError(self.error)

ValueError: An error occurred while retrieving token from XML response: AADSTS90023: Invalid STS request.

I have looked online and this appears to be standard code. I just copied the link from the homepage of my SharePoint and pasted it to create the url variable, so I do not think the problem is there either. Thank you in advance.

poroc300 avatar Jan 22 '24 16:01 poroc300

Greetings,

url parameter in AuthenticationContext should refer to site url instead of page url:

url = "https://{netorg_number}.sharepoint.com/sites/{company_name}"

vgrem avatar Jan 22 '24 22:01 vgrem

Thank you for the quick reply. I have also tested with that URL format and it failed with the same error. Is there any configuration issue that may raise this error?

poroc300 avatar Jan 23 '24 08:01 poroc300

Hi, I have the same issue as well when i use the above code with the url :https://netorgft14567460-my.sharepoint.com/personal/{organization}

I am trying to connect to my onedrive for business and the link is given below https://netorgft14567460-my.sharepoint.com/personal/{organization}/_layouts/15/onedrive.aspx What needs to be done here? Appreciate some solution!

muizzhaque avatar Jan 24 '24 08:01 muizzhaque

Hi @vgrem,

Is it possible that a configuration step is missing here before running this code? In which scenarios would you expect to see this error? Thank you.

poroc300 avatar Feb 12 '24 15:02 poroc300