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

Sharepoint Binary Security Token

Open cwripley1 opened this issue 4 years ago • 31 comments

Hello, My goal is given a sharepoint link to a site folder, to list the files, and to copy any new files automatically to AWS S3. When I run the simple startup script, I am getting the error:

Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf
Traceback (most recent call last):
  File "/Users/riple2/PycharmProjects/os365/microsoft/test5.py", line 10, in <module>
    response = ctx.execute_request_direct(request)
  File "/Users/riple2/Library/Python/3.8/lib/python/site-packages/office365/runtime/client_runtime_context.py", line 134, in execute_request_direct
    return self.pending_request().execute_request_direct(request)
  File "/Users/riple2/Library/Python/3.8/lib/python/site-packages/office365/runtime/odata/odata_request.py", line 34, in execute_request_direct
    return super(ODataRequest, self).execute_request_direct(request)
  File "/Users/riple2/Library/Python/3.8/lib/python/site-packages/office365/runtime/client_request.py", line 86, in execute_request_direct
    self.context.authenticate_request(request_options)
  File "/Users/riple2/Library/Python/3.8/lib/python/site-packages/office365/sharepoint/client_context.py", line 153, in authenticate_request
    self._auth_context.authenticate_request(request)
  File "/Users/riple2/Library/Python/3.8/lib/python/site-packages/office365/runtime/auth/authentication_context.py", line 84, in authenticate_request
    self._provider.authenticate_request(request)
  File "/Users/riple2/Library/Python/3.8/lib/python/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 73, in authenticate_request
    self.ensure_authentication_cookie()
  File "/Users/riple2/Library/Python/3.8/lib/python/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 "/Users/riple2/Library/Python/3.8/lib/python/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 95, in get_authentication_cookie
    token = self._acquire_service_token()
  File "/Users/riple2/Library/Python/3.8/lib/python/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 172, in _acquire_service_token
    token = self._process_service_token_response(response)
  File "/Users/riple2/Library/Python/3.8/lib/python/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 207, in _process_service_token_response
    raise ValueError(self.error)
ValueError: Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf

The simple script I am running is:

import json
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.http.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext
site_url="https://levi.sharepoint.com/sites/gdo/"
with open('secrets.json') as f:
  secrets = json.load(f)
ctx = ClientContext(site_url).with_credentials(UserCredential(secrets['user'], secrets['password']))
request = RequestOptions("{0}/_api/web/".format(site_url))
response = ctx.execute_request_direct(request)
json = json.loads(response.content)
web_title = json['d']['Title']
print("Web title: {0}".format(web_title))

I am using my Levi organization's username and password to the same site https://levi.sharepoint.com/sites/gdo/ . I can easily access this site in my browser, but the python package is having this authentication error.

What is needed besides username and password to get programatic access to sharepoint folders?

cwripley1 avatar Mar 24 '21 20:03 cwripley1

Did anyone figure out a way to handle this issue?

vatsaldesai1994 avatar Aug 03 '21 18:08 vatsaldesai1994

I am running into the same issue.

themattmorris avatar Aug 11 '21 15:08 themattmorris

I too got this error. I was using just my "username". Using "[email protected]" worked for me.

vj68 avatar Aug 30 '21 12:08 vj68

I was also having a similar issue when using environment variables. Turns out that os.getenv("USERNAME") will return my computer's username, not the USERNAME variable as specified in my .env file. I renamed the variable in my .env file to USER and it worked.

s-corbett avatar Nov 10 '21 17:11 s-corbett

Please provide a solution for this error,I have tried using the mail as the username..but still i see this error.

RPReddy avatar Jan 23 '22 14:01 RPReddy

Please provide a solution for this error,I have tried using the mail as the username..but still i see this error. @RPReddy , as mentioned by the @rajvijay68 , use the your email address and password, I got the same error which resolved by that method .

bhargav933 avatar Feb 21 '22 04:02 bhargav933

Anyone fixed the issue?

linkxu1989 avatar Mar 16 '22 12:03 linkxu1989

Only issue could be with your username value it should be proper email '[email protected]' check this properly.

ikothidar avatar May 23 '22 18:05 ikothidar

Hello, has anyone solve it? I face the same error when running the script from Lambda, but not from my local computer. That may me think that it may have something to do with the cookies thing in Lambda (I don't know much about this). I would appreciate any guidence

juangallegonuvu avatar Jul 13 '22 00:07 juangallegonuvu

This is an on going issue, if you have Duo enabled on a account, getting the token is not possible

babuganesh2000 avatar Aug 26 '22 19:08 babuganesh2000

I was getting this problem when I used firstname.lastname as the first argument to UserCredential. Using the full user id like [email protected] got me past the Cannot get binary security token exception.

I got much further and managed to get a 403 error when trying to upload a file but that's a different problem.

mrtoadsc avatar Oct 19 '22 22:10 mrtoadsc

still getting this error in 2023. Any updates? I tried using [email protected] for username but same error: Cannot get binary security token.

DieGlueckswurst avatar Mar 22 '23 09:03 DieGlueckswurst

For me it works perfectly when running my script as full username: [email protected] in any Linux computer I use, but I'm trying to containerize my app, and in any docker container I run it I get this same error.

Still not finding something to fix this issue in docker container.

leadvic avatar Mar 30 '23 21:03 leadvic

Currently running into the same issue running in a Jupyter notebook from a docker container.

Will investigate behaviour in a direct python script tomorrow.

jsandroos avatar Apr 09 '23 20:04 jsandroos

I'm having the same issue. I've had a system running for 2 years using shareplum to download a file from sharepoint. A couple of weeks ago it started throwing an error on the download. Doing some research I found that shareplum is not longer actively supported and decided to switch to Office365-REST-Python-Client, which doesn't work.

Since shareplum worked fine for years, and just started breakiing, I'm wondering if this is indicative of anything in my O365 account that our admins changed. Any idea about what would prevent username/password authentication?

cestes avatar Apr 11 '23 20:04 cestes

@cestes - were you able to resolve the issue? I have been running a weekly python script for several weeks now that uploads data into my SharePoint site. It also uses the shareplum library. The script stopped working on May 15th, 2023.

kg208 avatar May 25 '23 22:05 kg208

This way worked:

import json
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.http.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext

site_url= ""
client_id = ""
client_secret = ""

ctx = ClientContext(site_url).with_credentials(UserCredential("[email protected]", "password"))
endpoint_url = "{0}/_api/web/".format(site_url)
response = ctx.execute_request_direct(endpoint_url)

The error you are encountering is due to passing a RequestOptions object to the execute_request_direct method instead of a string representing the endpoint URL. The execute_request_direct method expects a string containing the endpoint URL to which the request should be made.

To fix this issue, you need to pass the URL as a string instead of a RequestOptions object. Here's the corrected code:

VictorlBueno avatar Aug 01 '23 11:08 VictorlBueno

Can anyone confirm if this error ('Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf') occurs due to a limitation of the Microsoft Developer free account? I am referring to this program: https://developer.microsoft.com/en-us/microsoft-365/dev-program.

ValberRodr avatar Jan 14 '24 02:01 ValberRodr

@ValberRodr Same things is also happaning to me also, I have currently microsoft 365 free account but not able to get token, Don't sure that's due free account.

masum13 avatar Jan 19 '24 13:01 masum13

@ValberRodr @masum13 same issue with me also

Piyush-CompufyTechnolab avatar Jan 19 '24 13:01 Piyush-CompufyTechnolab

i haven't tried to troubleshoot the issue for a few months now. hate to admit but i gave up on trying to resolve this 😳On Jan 19, 2024, at 06:10, Piyush-CompufyTechnolab @.***> wrote: @ValberRodr @masum13 same issue with me also

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>

kg208 avatar Jan 19 '24 15:01 kg208

For anyone else running into a similar problem: Mult-factor Authentication? https://learn.microsoft.com/en-us/answers/questions/1056853/fail-to-access-sharepoint-with-user-credentials

konnerthg avatar Feb 02 '24 09:02 konnerthg

Is this issue resolved? as I'm also getting the same error. Any documentation or link would be appreciated. Tried both the types of username

srishti1004 avatar Feb 15 '24 14:02 srishti1004

I received this error today while testing out Sharepoint access from a desktop environment. I was thinking it was due to the Python client codebase attempting to access the cloud, while my org's servers are all on-premise and have no cloud registration. But if it is some lesser issue I would love to see that.

I tried VictorIBueno's suggestion, but unfortunately received the same error.

This way worked:

import json
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.http.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext

site_url= ""
client_id = ""
client_secret = ""

ctx = ClientContext(site_url).with_credentials(UserCredential("[email protected]", "password"))
endpoint_url = "{0}/_api/web/".format(site_url)
response = ctx.execute_request_direct(endpoint_url)

The error you are encountering is due to passing a RequestOptions object to the execute_request_direct method instead of a string representing the endpoint URL. The execute_request_direct method expects a string containing the endpoint URL to which the request should be made.

To fix this issue, you need to pass the URL as a string instead of a RequestOptions object. Here's the corrected code:

sanerspace avatar Feb 15 '24 22:02 sanerspace

If you open the https://login.microsoftonline.com/extSTS.srf It says 'The endpoint only accepts POST requests. Received a GET request'. Am I missing something? @vgrem

yasasalwis avatar Mar 21 '24 21:03 yasasalwis

Disabling security defaults from Entra admin center worked for me - https://learn.microsoft.com/en-us/entra/fundamentals/security-defaults

hitarthnanonets avatar Apr 01 '24 10:04 hitarthnanonets