Office365-rest-python-client: IndexError: list index out of range

Created on 8 Dec 2020  ·  14Comments  ·  Source: vgrem/Office365-REST-Python-Client

DEar Sir,
Could you please help me to find out why I'm not connecting to sharepoint anymore?

With version 2.2.2 everything was working fine.
Then issue #272 occured (An error occurred while retrieving token from XML response: AADSTS500069 ) so I upgraded to 2.3.0.1.
Now the following code doesn't work either:
def copy_sharepoint(self,filename=""):
ctx = ClientContext(self.SITE_SHAREPOINT).with_credentials(UserCredential(self.WIN_USER,self.WIN_PASSWORD))
localpath=os.path.join(self.CHEMIN_XLSX ,self.NOM_TIR + '/',self.FICHIER_BI)
self.print_debug("",localpath)
self.print_debug("",self.WIN_PASSWORD)
with open(localpath, 'rb') as content_file:
file_content = content_file.read()
file = ctx.web.get_folder_by_server_relative_url(self.CHEMIN_SHAREPOINT).upload_file(self.FICHIER_BI, file_content).execute_query()

Traceback (most recent call last):
File "/appli/sopra/tools/tir_bench.py", line 1327, in
tir_bench.copy_sharepoint()
File "/appli/sopra/tools/tir_bench.py", line 1278, in copy_sharepoint
file = ctx.web.get_folder_by_server_relative_url(self.CHEMIN_SHAREPOINT).upload_file(self.FICHIER_BI, file_content).execute_query()
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/client_object.py", line 30, in execute_query
self.context.execute_query()
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/client_runtime_context.py", line 138, in execute_query
self.pending_request().execute_query()
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/client_request.py", line 73, in execute_query
self.beforeExecute.notify(request)
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/types/EventHandler.py", line 18, in notify
listener(args, *kwargs)
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/sharepoint/client_context.py", line 162, in _build_modification_query
self.ensure_form_digest(request)
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/sharepoint/client_context.py", line 123, in ensure_form_digest
self.request_form_digest()
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/sharepoint/client_context.py", line 130, in request_form_digest
response = self.execute_request_direct(request)
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/client_runtime_context.py", line 134, in execute_request_direct
return self.pending_request().execute_request_direct(request)
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/odata/odata_request.py", line 34, in execute_request_direct
return super(ODataRequest, self).execute_request_direct(request)
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/client_request.py", line 86, in execute_request_direct
self.context.authenticate_request(request_options)
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/sharepoint/client_context.py", line 151, in authenticate_request
self._auth_context.authenticate_request(request)
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/auth/authentication_context.py", line 84, in authenticate_request
self._provider.authenticate_request(request)
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 73, in authenticate_request
self.ensure_authentication_cookie()

File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 80, in ensure_authentication_cookie
self._cached_auth_cookies = self.get_authentication_cookie()
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 93, in get_authentication_cookie
token = self._acquire_service_token_from_adfs(user_realm.STSAuthUrl)
File "/home/benchsp/.local/lib/python3.9/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 136, in _acquire_service_token_from_adfs
assertion_node = dom.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion", 'Assertion')[0].toxml()
IndexError: list index out of range

Could you please help me with that issue?

Thanks in advance,
Guillaume

bug

All 14 comments

Hello,
In addition, here's the value of response.content.decode())

http://www.w3.org/2005/08/addressing/soap/faultcfb81633-ff49-4e50-9544-7e39214d7122s:Sendera:InvalidSecurityAn error occurred when verifying security for the message.

Greetings!

thank you for the detailed log, at the moment im lacking any possibility to verify it against SharePoint/ADFS env.

@wreiner, maybe you could kindly confirm whether _federated authentication_ is working as expected on your side for the latest release?

Hi everyone

I am having similar issues, the following line gives me a 400 or 500 response.

https://github.com/vgrem/Office365-REST-Python-Client/blob/020c8c8176ce2eea3accbe95e2e4372af57ef6c8/office365/runtime/auth/providers/saml_token_provider.py#L133

In case I get the 400 response the body is empty resulting in a xml.parsers.expat.ExpatError: no element found: line 1, column 0 exception. the 500 response gives same exception as @guillaume-colas.

I have not yet gotten the package to work, so it could well be the sharepoint server, but I will try downgrading the package just to be sure. How can I ensure that sharepoint authentication is setup correctly?

Thx in advance!

Hi,

I'm not at my computer to fully analyse but as I see it, when I install the latest version with pip the function _acquire_service_token_from_adfs_ is missing from _runtime/auth/providers/saml_token_provider.py_.

In my pull request there is no removal of this function so I'm not sure why it is missing. Also my fix for the datetime format is not in the file. Maybe there was an error with the merge?

@wreiner thank you for getting back, regarding

acquire_service_token_from_adfs is missing

its been refactored from public to private method.

my fix for the datetime format is not in the file

the format should be in place as were committed originally, but its been refactored abit in terms of moving the declaration to sts_profile.py

self.created = now.astimezone(timezone.utc).isoformat('T')[:-9] + 'Z' self.expires = (now + timedelta(minutes=10)).astimezone(timezone.utc).isoformat('T')[:-9] + 'Z'

But if something is missing after the merge, i should be blamed.

Hi everyone

I am having similar issues, the following line gives me a 400 or 500 response.

https://github.com/vgrem/Office365-REST-Python-Client/blob/020c8c8176ce2eea3accbe95e2e4372af57ef6c8/office365/runtime/auth/providers/saml_token_provider.py#L133

In case I get the 400 response the body is empty resulting in a xml.parsers.expat.ExpatError: no element found: line 1, column 0 exception. the 500 response gives same exception as @guillaume-colas.

I have not yet gotten the package to work, so it could well be the sharepoint server, but I will try downgrading the package just to be sure. How can I ensure that sharepoint authentication is setup correctly?

Thx in advance!

Hi Mark,

thank you for confirming it, it indeed appears in the latest version federated authentication (SharePoint/ADFS) somewhat broken
Could you please give it a try for the _previous_ version (2.2.2)?

I'm not through complete analysis but for the my changes I can say they return the security token and cookie correctly but the cookie jar does not contain a 'FedAuth' field which is looked for in is_valid_auth_cookies which is now being used in _get_authentication_cookie.

https://github.com/vgrem/Office365-REST-Python-Client/blob/93325ea33675b86aafc2e6e0fa381c184ce479e7/office365/runtime/auth/providers/saml_token_provider.py#L32

For federated auth I only see SPOIDCRL in the jar.

Currently I'm still unable to use the obtained auth data correctly, I'm still investigating but can't tell how long I'll need.

All in all I can tell that with the old version and my patches everything still works fine as expected but unfortunately not with the new version.

Thanks for the quick responses :-)

@vgrem it get a similar error on version 2.2.2, so it seems to be the sharepoint server.

It might be nice to wrap the exception into a more readable one.

I am one step further. Turns out I needed to reset my password. I'm now hitting the same error as @wreiner. The is_valid_auth_cookies returns that the cookie is not valid, because the dictionary only contains the SPOIDCRL key and no FedAuth key. If i just comment out this check on whether the cookie is valid or not. I am able to download files from sharepoint. So the authentication seems to work fine.

Maybe I'll try version 2.2.2 again.

I have the same issue. is_valid_auth_cookies return False because of no FedAuth key in cookies dict. If I comment return code and simplify return True, authentication will work fine.

def is_valid_auth_cookies(values):
    return True
    # return any(values) and values.get('FedAuth', None) is not None

Hey @hungnguyen95 , thank you for confirming it!

Turns out is_valid_auth_cookies needs to be adjusted.

Hello,
Will my issue be fixed by modifying is_valid_auth_cookies ?

I'm quite lost here sorry. I thought that my issue was in _acquire_service_token_from_adfs.
Do you have any idea why I'm getting response 'Invalid security' and hopefully a workaround to get authentifcated?

Thanks in advance for your help.
Guillaume

@guillaume-colas it seems that your issue is probably related to the server not allowing you access. Are you sure the password is correct and have you tried resetting your password? Resetting my password resolved this issue. After which I ran into the is_valid_auth_cookies issue.

@mark-boer Yes I've verified my password and it's correct. Is it possible that's the authentification with 2FA (sms code) is blocking from authentificating?

Is it possible to use ClientCredentials to upload files to sharepoint?

Thanks in advance for your help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

continda picture continda  ·  4Comments

haimat picture haimat  ·  5Comments

attibalazs picture attibalazs  ·  10Comments

liuliqiu picture liuliqiu  ·  7Comments

ahulist picture ahulist  ·  5Comments