KeyError: 'FedAuth' when aquireing user token
I want to fetch a file from a SharePoint website. So first, I tried to run the exemple code found in the doc replacing username, password and the site with my company's website like here:
import os
from getpass import getpass
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
password = getpass("Password: ")
site_url = "https://company.sharepoint.com"
ctx = ClientContext(site_url).with_credentials(UserCredential(os.getenv('USERNAME'), password))
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web title: {0}".format(web.properties['Title']))
When I run it, I enter my password, press enter and it prints this: Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf
Here's the full error message:
Traceback (most recent call last):
File "C:/cygwin-portable/cygwin/home/User/Projects/myproject/run.py", line 21, in <module>
ctx.execute_query()
File "C:\cygwin-portable\cygwin\home\User\Projects\myproject\venv\lib\site-packages\office365\runtime\client_runtime_context.py", line 140, in execute_qu
ery
self.pending_request().execute_query()
File "C:\cygwin-portable\cygwin\home\User\Projects\myproject\venv\lib\site-packages\office365\runtime\client_request.py", line 74, in execute_query
response = self.execute_request_direct(request)
File "C:\cygwin-portable\cygwin\home\User\Projects\myproject\venv\lib\site-packages\office365\runtime\odata\odata_request.py", line 34, in execute_reques
t_direct
return super(ODataRequest, self).execute_request_direct(request)
File "C:\cygwin-portable\cygwin\home\User\Projects\myproject\venv\lib\site-packages\office365\runtime\client_request.py", line 86, in execute_request_dir
ect
self.context.authenticate_request(request_options)
File "C:\cygwin-portable\cygwin\home\User\Projects\myproject\venv\lib\site-packages\office365\runtime\client_runtime_context.py", line 66, in authenticat
e_request
self._auth_context.authenticate_request(request)
File "C:\cygwin-portable\cygwin\home\User\Projects\myproject\venv\lib\site-packages\office365\runtime\auth\authentication_context.py", line 70, in authen
ticate_request
self.acquire_token_func()
File "C:\cygwin-portable\cygwin\home\User\Projects\myproject\venv\lib\site-packages\office365\sharepoint\client_context.py", line 124, in _acquire_token
return self.authentication_context.acquire_token_for_user(credentials.userName,
File "C:\cygwin-portable\cygwin\home\User\Projects\myproject\venv\lib\site-packages\office365\runtime\auth\authentication_context.py", line 37, in acquir
e_token_for_user
if not self.provider.acquire_token():
File "C:\cygwin-portable\cygwin\home\User\Projects\myproject\venv\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 70, in
acquire_token
return self._acquire_authentication_cookie(token, user_realm.IsFederated)
File "C:\cygwin-portable\cygwin\home\User\Projects\myproject\venv\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 234, i
n _acquire_authentication_cookie
self._auth_cookies[name] = cookies[name]
KeyError: 'FedAuth'
Thanks in advance
Greetings,
according to trace log, it seems it fails on acquiring a security token, revealing some additional details might help to pin-point the root cause:
- is federated account is involved to connect to SharePoint?
- whether MFA is enabled
Could you give it a try with App-only flow instead of user credentials flow?
Here is a step-by-step instruction on how to configure app principal and grant access.
Hello,
I had the same problem, KeyError: 'FedAuth'.
Try using AuthenticationContext instead of UserCredential
from office365.runtime.auth.authentication_context import AuthenticationContext from office365.sharepoint.client_context import ClientContext
site_url = "https://company.sharepoint.com" ctx_auth = AuthenticationContext(url=site_url) ctx_auth.acquire_token_for_user(username=user, password=password)
ctx = ClientContext(site_url, ctx_auth)
I had the same problem and resolved it using app principal as described in this issue: #191
The problem is with authentication. In my case, I couldn't use my credentials (user and password) because of the way my company setup the access to the Sharepoint.
The solution can be found in the section "Setting up an app-only principal with tenant permissions" of https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
Once you have access to client_id and client_secret, use them to create the authentication. Please, see the example below:
from office365.sharepoint.client_context import ClientContext from office365.runtime.auth.authentication_context import AuthenticationContext
client_id="{client id}" client_secret="{client secret}" url = "https://{tenant}.sharepoint.com/sites/{site}"
ctx_auth = AuthenticationContext(url) if ctx_auth.acquire_token_for_app(client_id, client_secret): ctx = ClientContext(url, ctx_auth) web = ctx.web ctx.load(web) ctx.execute_query() print("Web title: {0}".format(web.properties['Title']))
else: print(ctx_auth.get_last_error())