I am trying to upload files to SharePoint through Python. I have tried the following:
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
def get_sharepoint_context_using_user():
sharepoint_url = 'https://companyname.sharepoint.com/sites/Afolder/'
user_credentials = UserCredential("login", "password")
ctx = ClientContext(sharepoint_url).with_credentials(user_credentials)
return ctx
def upload_to_sharepoint(file_name: str):
sp_relative_url='https://companyname.sharepoint.com/sites/Afolder/some%20thing/Forms/AllItems.aspx'
ctx = get_sharepoint_context_using_user()
ctx = get_sharepoint_context_using_user()
target_folder = ctx.web.get_folder_by_server_relative_url(sp_relative_url)
with open(file_name, 'rb') as content_file:
file_content = content_file.read()
target_folder.upload_file(file_name, file_content).execute_query()
After I try to call the function of uploading
upload_to_sharepoint(os.path.join(path_from, file_word))
I got the following mistake:
ValueError: Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf
Perhaps, you know what might be the reason of it. I tried different variations, but it did not work. The tricky part is that I was granted an access not to the whole folder, I mean https://companyname.sharepoint.com, but to the part of it beginning from https://companyname.sharepoint.com/sites/Afolder. Could it be the reason of this problem? If yes, is there any opportunity to upload to folders through Python, when there is such a limited access.
Thank you in advance!
The thing is that this error is more of a problem with the authentication credentials when the authentication token cannot be retrieved from the specified URL. So in general terms, not only that the folder does not have the authorization.