vgrem / Office365-REST-Python-Client

Microsoft 365 & Microsoft Graph Library for Python
MIT License
1.35k stars 336 forks source link

getting error while executing "result = search.post_query(request).execute_query()" in https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/search/search_documents.py #593

Open balapoluru opened 2 years ago

balapoluru commented 2 years ago

Error: PS H:\a\b\c\d> python .\test_sharepoint.py <office365.sharepoint.search.request.SearchRequest object at 0x0000026D9F3BB8B0> Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf Traceback (most recent call last): File "H:\a\b\c\d\test_sharepoint.py", line 26, in result = search.post_query(request).execute_query() File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\client_result.py", line 38, in execute_query self._context.execute_query() File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\client_runtime_context.py", line 151, in execute_query self.pending_request().execute_query() File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\client_request.py", line 73, in execute_query response = self.execute_request_direct(request) File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\odata\request.py", line 36, in execute_request_direct return super(ODataRequest, self).execute_request_direct(request) File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\client_request.py", line 85, in execute_request_direct self.beforeExecute.notify(request) File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\types\event_handler.py", line 21, in notify listener(*args, **kwargs) File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\sharepoint\client_context.py", line 233, in _build_modification_query self.ensure_form_digest(request) File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\sharepoint\client_context.py", line 166, in ensure_form_digest self._ctx_web_info = self._get_context_web_information() File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\sharepoint\client_context.py", line 173, in _get_context_web_information response = self.pending_request().execute_request_direct(request) File "C:\a\b\c\d\python3.10\latest\lib\site-packages\office365\runtime\odata\request.py", line 36, in execute_request_direct return super(ODataRequest, self).execute_request_direct(request) File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\client_request.py", line 86, in execute_request_direct self.context.authenticate_request(request) File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\sharepoint\client_context.py", line 219, in authenticate_request self.authentication_context.authenticate_request(request) File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\auth\authentication_context.py", line 97, in authenticate_request self._provider.authenticate_request(request) File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 77, in authenticate_request self.ensure_authentication_cookie() File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 84, in ensure_authentication_cookie self._cached_auth_cookies = self.get_authentication_cookie() File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 99, in get_authentication_cookie token = self._acquire_service_token() File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 176, in _acquire_service_token token = self._process_service_token_response(response) File "C:\a\b\c\d\tools\python3.10\latest\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 211, in _process_service_token_response raise ValueError(self.error) ValueError: Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf PS H:\a\b\c\d>

ClearSafety commented 2 weeks ago

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())