Hi RaytheonXie, thanks for your reply.
During my research, I saw the link that you shared. To make it works, I think that I need access to the Azure AD, is that right? Cause looking at the path described in the documentation (Azure portal > Azure AD), the following page appears to me:
Ok, so I tried to use the second way to authenticate, with the code that you shared, and I got the following error:
C:\Users\<user's folder>\codes\Office365-REST-Python-Client>py office_test.py
Traceback (most recent call last):
File "C:\Users\<user's folder>\codes\Office365-REST-Python-Client\office_test.py", line 4, in <module>
from examples import acquire_token_by_username_password
File "C:\Users\<user's folder>\codes\Office365-REST-Python-Client\examples\__init__.py", line 5, in <module>
from tests import settings
File "C:\Users\<user's folder>\codes\Office365-REST-Python-Client\tests\__init__.py", line 18, in <module>
class SecEnvInterpolation(BasicInterpolation):
File "C:\Users\<user's folder>\codes\Office365-REST-Python-Client\tests\__init__.py", line 19, in SecEnvInterpolation
secure_vars = os.environ.get('office365_python_sdk_securevars').split(';')
AttributeError: 'NoneType' object has no attribute 'split'
I tried to change the settings.cfg file under /test folder but no success so far.
EDIT:
I finally got it working for the files inside my company's SharePoint using 'UserCredential' but I'm still not able to access shared files from others' Sharepoint.
For example, my user has a site under "xpto.sharepoint.com" and, from the browser it has access to the file abcd.sharepoint.com/:u:/p/other_usersspace/file3.txt. But, even after retrieving the context from the site URL, the following error appears:
<office365.sharepoint.client_context.ClientContext object at 0x000001F99427B0A0>
An error occurred while retrieving auth cookies from https://abcd.sharepoint.com/_forms/default.aspx?wa=wsignin1.0
Traceback (most recent call last):
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office_bruno.py", line 38, in <module>
file = ctx.web.get_file_by_server_relative_url(file_url).download(local_file).execute_query()
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\runtime\client_object.py", line 44, in execute_query
self.context.execute_query()
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\runtime\client_runtime_context.py", line 161, in execute_query
self.pending_request().execute_query(qry)
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\runtime\client_request.py", line 57, in execute_query
response = self.execute_request_direct(request)
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\runtime\client_request.py", line 69, in execute_request_direct
self.beforeExecute.notify(request)
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\runtime\types\event_handler.py", line 21, in notify
listener(*args, **kwargs)
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\sharepoint\client_context.py", line 212, in _authenticate_request
self.authentication_context.authenticate_request(request)
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\runtime\auth\authentication_context.py", line 97, in authenticate_request
self._provider.authenticate_request(request)
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\runtime\auth\providers\saml_token_provider.py", line 77, in authenticate_request
self.ensure_authentication_cookie()
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\runtime\auth\providers\saml_token_provider.py", line 84, in ensure_authentication_cookie
self._cached_auth_cookies = self.get_authentication_cookie()
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\runtime\auth\providers\saml_token_provider.py", line 100, in get_authentication_cookie
return self._get_authentication_cookie(token, user_realm.IsFederated)
File "C:\Users\<user>\codes\Office365-REST-Python-Client\office365\runtime\auth\providers\saml_token_provider.py", line 246, in _get_authentication_cookie
raise ValueError(self.error)
ValueError: An error occurred while retrieving auth cookies from https://abcd.sharepoint.com/_forms/default.aspx?wa=wsignin1.0
The code that is working inside the company is something like the following and the error is happening :
import os
import tempfile
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.user_credential import UserCredential
user_credentials = UserCredential('<user>','<pass>')
url = "https://abcd.sharepoint.com/personal/other_usersspace/file3.txt"
file_url = "personal/other_usersspace/file3.txt"
print(f'file_url: {file_url}')
ctx = ClientContext(site_url).with_credentials(user_credentials)
print(ctx)
download_path = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
with open(download_path, "wb") as local_file:
file = ctx.web.get_file_by_server_relative_url(file_url).download(local_file).execute_query()
print("[Ok] file has been downloaded into: {0}".format(download_path))
Thanks again.
Bruno