fail to access sharepoint with user credentials

amit cahanovich 1 Reputation point
2022-10-20T17:59:14.343+00:00

hi,
the following code works for me for one tenant but not to the other tenant (the user credentials works with the browser)
to emphasise, this is my customer site, and i dont have permissions on https://xxxxx.sharepoint.com, but only on the site itself
i use latest (Office365-REST-Python-Client-2.3.14) on python 3.8 and python 3.9

site_url = "https://xxxxx.sharepoint.com/sites/xxxxxxx"
username = "xxxxxi"
password = "xxxxxxxx"
from office365.sharepoint.client_context import ClientContext

ctx = ClientContext(site_url)
ctx.with_user_credentials(username,password )

web = ctx.web.get().execute_query()
print(web.url)

stack

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)
<ipython-input-15-49ba75f73138> in <module>
11 ctx.with_user_credentials(username,password )
12
---> 13 web = ctx.web.get().execute_query()
14 print(web.url)
15

/usr/local/anaconda3/lib/python3.8/site-packages/office365/runtime/client_object.py in execute_query(self)
42 :type self: T
43 """
---> 44 self.context.execute_query()
45 return self
46

/usr/local/anaconda3/lib/python3.8/site-packages/office365/runtime/client_runtime_context.py in execute_query(self)
144 def execute_query(self):
145 """Submit request(s) to the server"""
--> 146 self.pending_request().execute_query()
147
148 def add_query(self, query):

/usr/local/anaconda3/lib/python3.8/site-packages/office365/runtime/client_request.py in execute_query(self)
72 request = self.build_request(qry)
73 self.beforeExecute.notify(request)
---> 74 response = self.execute_request_direct(request)
75 response.raise_for_status()
76 self.process_response(response)

/usr/local/anaconda3/lib/python3.8/site-packages/office365/runtime/odata/request.py in execute_request_direct(self, request)
34 """
35 self._build_specific_request(request)
---> 36 return super(ODataRequest, self).execute_request_direct(request)
37
38 def build_request(self, query):

/usr/local/anaconda3/lib/python3.8/site-packages/office365/runtime/client_request.py in execute_request_direct(self, request)
84 :type request: office365.runtime.http.request_options.RequestOptions
85 """
---> 86 self.context.authenticate_request(request)
87 if request.method == HttpMethod.Post:
88 if request.is_bytes or request.is_file:

/usr/local/anaconda3/lib/python3.8/site-packages/office365/sharepoint/client_context.py in authenticate_request(self, request)
236
237 def authenticate_request(self, request):
--> 238 self.authentication_context.authenticate_request(request)
239
240 def _build_modification_query(self, request):

/usr/local/anaconda3/lib/python3.8/site-packages/office365/runtime/auth/authentication_context.py in authenticate_request(self, request)
88 :type request: office365.runtime.http.request_options.RequestOptions
89 """
---> 90 self._provider.authenticate_request(request)

/usr/local/anaconda3/lib/python3.8/site-packages/office365/runtime/auth/providers/saml_token_provider.py in authenticate_request(self, request)
75 """
76 logger = self.logger(self.authenticate_request.name)
---> 77 self.ensure_authentication_cookie()
78 logger.debug_secrets(self._cached_auth_cookies)
79 cookie_header_value = "; ".join(["=".join([key, str(val)]) for key, val in self._cached_auth_cookies.items()])

/usr/local/anaconda3/lib/python3.8/site-packages/office365/runtime/auth/providers/saml_token_provider.py in ensure_authentication_cookie(self)
82 def ensure_authentication_cookie(self):
83 if self._cached_auth_cookies is None:
---> 84 self._cached_auth_cookies = self.get_authentication_cookie()
85 return True
86

/usr/local/anaconda3/lib/python3.8/site-packages/office365/runtime/auth/providers/saml_token_provider.py in get_authentication_cookie(self)
98 else:
99 token = self._acquire_service_token()
--> 100 return self._get_authentication_cookie(token, user_realm.IsFederated)
101 except requests.exceptions.RequestException as e:
102 logger.error(e.response.text)

/usr/local/anaconda3/lib/python3.8/site-packages/office365/runtime/auth/providers/saml_token_provider.py in _get_authentication_cookie(self, security_token, federated)
244 self._sts_profile.signin_page_url)
245 logger.error(self.error)
--> 246 raise ValueError(self.error)
247 return cookies
248

ValueError: An error occurred while retrieving auth cookies from https://xxxxx.sharepoint.com/_forms/default.aspx?wa=wsignin1.0

any idea ?
Thanks,
Amit

Microsoft 365 and Office SharePoint Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-10-21T08:41:06.553+00:00

    Hi @amit cahanovich
    Please check if your account have enabled MFA first. You will need verification code when you log in sharepoint if you enabled MFA.
    Here is the document for more details
    https://learn.microsoft.com/en-us/microsoft-365/admin/security-and-compliance/set-up-multi-factor-authentication?view=o365-worldwide

    You can disable MFA or use apponly to access sharepoint. Please refer to following document to register app only
    https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.