I am trying to upload files to a SharePoint folder structure which has been already created, I have used two files for this Config_template:

config = dict()
config['sp_user'] = '[email protected]'
config['sp_password'] = 'pass@123'
config['sp_base_path'] = 'https://xyz.sharepoint.com'
config['sp_site_name'] = '/sites/bootromandhsesecurity/Shared%20Documents/Forms/AllItems.aspx?originalPath=0d66ed181f1&id=%2Fsites%2Fbootromandhsesecurity%2FShared%20Documents%2F%2FSprint3'
config['sp_doc_library'] = 'Sprint3'

The next file is sharepoint_upload:

import requests
from shareplum import Office365
from .config_template import config
def up():
    # get data from configuration
    username = config['sp_user']
    password = config['sp_password']
    site_name = config['sp_site_name']
    base_path = config['sp_base_path']
    doc_library = config['sp_doc_library']

    file_name = "TestCaseDatabase\\Sprint3_Test_Report.xlsx"

    # Obtain auth cookie
    authcookie = Office365(base_path, username=username, password=password).GetCookies()
    session = requests.Session()
    session.cookies = authcookie
    session.headers.update({'user-agent': 'python_bite/v1'})
    session.headers.update({'accept': 'application/json;odata=verbose'})

    session.headers.update({'X-RequestDigest': 'FormDigestValue'})
    response = session.post( url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='a.txt',overwrite=true)",
    session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})

    # perform the actual upload
    with open( file_name, 'rb') as file_input:
            response = session.post( 
                url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='" 
                + file_name + "',overwrite=true)",
        except Exception as err: 
            print("Some error occurred: " + str(err))

The username and password have been changed here for security reasons, but when the correct details have been given,

Error authenticating against Office 365. Error from Office 365:', 'AADSTS50126: Error validating credentials due to invalid username or password

The above error occurs. Where am I going wrong?

P.S - I have been using Microsoft authenticator for logging into the SharePoint folder, where I need to approve my login on my mobile, is that causing the problem?

1 Answer 1


I never worked with Python, but my experience with connecting sharepoint with MFA enabled account is that you removed the Password parameter from the code.

On executing the Powershell/code (without password parameter), a pop up will come to provide the credentials and MFA unique code.

give it try..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.