Office365-rest-python-client: "TypeError: cannot concatenate 'str' and 'NoneType' objects" on request to get a file.

Created on 12 Jun 2017  ·  14Comments  ·  Source: vgrem/Office365-REST-Python-Client

I'm trying to download a file which is accessible (via a browser) on this URL:
https://test.sharepoint.com/sites/team/team documents/subfolder/document.docx

I'm using a ClientRequest object with the following code:

url = "https://test.sharepoint.com/sites/team"
username="[email protected]"
password="pass"
ctxAuth = AuthenticationContext(url)
if ctxAuth.acquire_token_for_user(username, password):
    print 'authentication successful, proceeding...'
    request = ClientRequest(ctxAuth)
    requestUrl="{0}/_api/web/getfilebyserverrelativeurl('team documents/subfolder/document.docx')"
    options=RequestOptions(requestUrl.format(url))
    data = request.execute_query_direct(options)

On running this, I'm getting the following trace:

$ python test.py
No handlers could be found for logger "client.office365.runtime.auth.saml_token_provider.SamlTokenProvider.process_service_token_response"
authentication successful, proceeding...
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    data = request.execute_query_direct(options)
  File "/usr/lib/python2.7/site-packages/client/office365/runtime/client_request.py", line 77, in execute_query_direct
    self.context.authenticate_request(request_options)
  File "/usr/lib/python2.7/site-packages/client/office365/runtime/auth/authentication_context.py", line 20, in authenticate_request
    request_options.set_header('Cookie', self.provider.get_authentication_cookie())
  File "/usr/lib/python2.7/site-packages/client/office365/runtime/auth/saml_token_provider.py", line 65, in get_authentication_cookie
    return 'FedAuth=' + self.FedAuth + '; rtFa=' + self.rtFa
TypeError: cannot concatenate 'str' and 'NoneType' objects

Is there something wrong on how I use the module?

bug

Most helpful comment

I had a similar problem because our SharePoint instance is running behind a proxy. I had to subclass the SamlTokenProvider to override the login url (self.sts) and cookie names. I think the issue is that during initialization, the code doesn't check the result of acquire_authentication_cookie so if authentication fails you don't hear about it until the cookies are used.

All 14 comments

I had a similar problem because our SharePoint instance is running behind a proxy. I had to subclass the SamlTokenProvider to override the login url (self.sts) and cookie names. I think the issue is that during initialization, the code doesn't check the result of acquire_authentication_cookie so if authentication fails you don't hear about it until the cookies are used.

I ran into the same issue. It seems like authentication failures are not handled by exceptions. It only bubbles up when cookies are queried. Check your credentials

Same issue. Checked credentials but issue persists. Any other thoughts? Thanks!

I ended up using OAuth2Session to get oauth2 authentication and grab data from graph api. its been a while and i no longer remember the exact details.

Thanks will check it out.

Hi, I have the same issue, but in my case I think it's because I have sharepoint configured with FormAuthentication method. No way it will work until the proper token provider is implemented

I am bumping into this problem too but only if I am using an outlook.com account. I am hoping somebody can explain what is going on behind the scenes and perhaps offer a solution. Here is a brief explanation:

1) Our SharePoint site is in the cloud (https://mn365.sharepoint.com)
2) When I make the connection to the site using my State of Minnesota credentials, it connects just fine.
3) But since I don't want to have my own credentials embedded in my application, I created a "service account" along the lines of "[email protected]". This account fails.

What would be the different in authentication between using an outlook.com address and one issued by my organization (in this case firstName.[email protected]?

Same problem here. We're using AD FS for authentication. Till now I didn't found any way to get any information from our sharepoint online server. I tried a lot of libraries but nothing worked for me.

I had the same error because sharepoint was redirecting for authentication to the on-prem STS/ADFS.

We created a new O365 user directly in O365 so it doesn't use the redirect to the on-prem STS/ADFS. This resolved the error 👍

I switched to sharepy, which runs ootb and covers my conditions

Same here:
No handlers could be found for logger "office365.runtime.auth.saml_token_provider.SamlTokenProvider._process_service_token_response"

Any luck from others?

Maybe the following helps to narrow it down further.

I can successfully authenticate on the general sharepoint URL, but not on a URL that is pointing to a specific teams-channel. The following bit of code works fine:

url = "https://COMPANYNAME.sharepoint.com/"
username=raw_input("username: ")
password=raw_input("password: ")
ctx_auth = AuthenticationContext(url=url)
if ctx_auth.acquire_token_for_user(username=username, password=password):

But when I replace the URL with:

url = "https://COMPANYNAME.sharepoint.com/sites/TEAMSCHANNEL/"

then I get the following error::

No handlers could be found for logger "office365.runtime.auth.saml_token_provider.SamlTokenProvider.acquire_authentication_cookie"

Any work-around appreciated - in my browser I can see both of them just fine.

We found a workaround for the above use case. Just use the base url for getting the AuthenticationContext. New ClientContext objects can be created using different urls.

base_url = "https://COMPANYNAME.sharepoint.com/"
ctx_auth = AuthenticationContext(url=base_url)
if not ctx_auth.acquire_token_for_user(username=user, password=password):
    sys.exit('Error in getting token - quitting')
url = base_url + "sites/TEAMSCHANNEL/"
ctx = ClientContext(url, ctx_auth)
..
..

Hey guys!

Since the moment when this issue was reported until now the following improvements/bug fixes have been made in terms of _support for SAML-based federated authentication with SharePoint Online_:

  • #210 - Unable to connect to SharePoint(acquire_authentication_cookie error if federated)
  • #170 - An error occurred while retrieving token: AADSTS50126: Error validating credentials due to invalid username or password
  • #85 - Using Office-365-REST with ADFS
  • #84 - Sites with on-premise STS does not authenticate (AADSTS70002)

So, the provided error should no longer occur, just make sure to grab the _latest_ version (2.1.10.1 or above).

Was this page helpful?
0 / 5 - 0 ratings