[Python] Error when downloading excel file from Sharepoint - "Cannot get binary security...

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to download a excel file from Sharepoint using python script but due to MFA my credentials are not working in the script now and I am getting error. So I tried to use Service Account credentials and then I am getting the error "ValueError: Cannot get binary security token for from https://XXXXmicrosoftXXX.com."

    Can anyone please advise how to overcome this error? Or alternate approach to download the file from Sharepoint?

    Below is the code that I am using:

    from office365.sharepoint.client_context import ClientContext
    from office365.sharepoint.files.file import File
    from office365.runtime.auth.user_credential import UserCredential

    # SharePoint site and file details
    site_url = "https://..."
    file_url = "file.xlsx"
    local_path = "path"

    # Credentials (username and password)
    username = "username"
    password = "pswd"

    user_credentials = UserCredential('username', 'pswd')

    # Authenticate and connect to SharePoint
    context = ClientContext(site_url).with_credentials(user_credentials)

    # Download file from SharePoint
    response = File.open_binary(context, file_url)

    # Save the file locally
    with open(local_path, "wb") as f:
    f.write(response.content)

    print(f"File {local_path} downloaded successfully.")

    Continue reading...

Compartilhe esta Página