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

Viewed 1732

I trying to download the files from Microsoft office 365 SharePoint folder , using Python script but got an exception " raise Exception('Error authenticating against Office 365. Error from Office 365:', message[0].text) Exception: ('Error authenticating against Office 365. Error from Office 365:', 'AADSTS50126: Error validating credentials due to invalid username or password.')" although I gave the correct user name and pass word. please help me to resolve this thank you

project.py

from sharepoint import SharePoint

# set file name
file_name = 'test.docx'

# set the folder name
folder_name = 'temp_for_test'

# get file
file  = SharePoint().download_file(file_name, folder_name)

# save file
with open(file_name, 'wb') as f:
    f.write(file)
    f.close()

config.json

{
    "share_point":
        {
            "user": "xyz@company.com",
            "password": "password",
            "url": "https://company.sharepoint.com",
            "site": "https://company.sharepoint.com/sites/OI-Platform/",
            "doc_library": "Shared Documents/general"
        }
}

sharepoint.py


from shareplum import Site, Office365
from shareplum.site import Version

import json, os

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
config_path = '\\'.join([ROOT_DIR, 'config.json'])

# read json file
with open(config_path) as config_file:
    config = json.load(config_file)
    config = config['share_point']

USERNAME = config['user']
PASSWORD = config['password']
SHAREPOINT_URL = config['url']
SHAREPOINT_SITE = config['site']
SHAREPOINT_DOC = config['doc_library']


class SharePoint:
    def auth(self):
        self.authcookie = Office365(SHAREPOINT_URL, username=USERNAME, password=PASSWORD).GetCookies()
        self.site = Site(SHAREPOINT_SITE, version=Version.v365, authcookie=self.authcookie)

        return self.site

    def connect_folder(self, folder_name):
        self.auth_site = self.auth()

        self.sharepoint_dir = '\\'.join([SHAREPOINT_DOC, folder_name])
        self.folder = self.auth_site.Folder(self.sharepoint_dir)

        return self.folder

    def download_file(self, file_name, folder_name):
        self._folder = self.connect_folder(folder_name)
        return self._folder.get_file(file_name)

1 Answers

If the credentials are correct but it's still not working, double check your url, site, and doc_library strings to ensure there are no spelling mistakes there.

If those are correct, double check that you have "v365" for your version. If it's different, that's probably where the error might be occurring.

Related