jasonrollins / shareplum

Pythonic SharePoint
MIT License
179 stars 96 forks source link

Error authenticating against Office 365. Error from Office 365: #84

Open odairvaz opened 4 years ago

odairvaz commented 4 years ago

Hello everyone, I'm trying to upload some files to SharePoint in Office 365 but the authentication fails even though that I put the right credentials.Here's the code:

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

#get all the csv files
csv_files = list(filter(lambda x: '.csv' in x, os.listdir('./csvfiles')))

# Office 365 Authentication
authcookie = Office365('https://tenant.sharepoint.com',
                       username='login@tenant.com', password='password').GetCookies()
site = Site('https://tenant.sharepoint.com/sites/Folder',
            version=Version.v365, authcookie=authcookie)

However, I do manage to log in and upload data with my personal account, but I can’t upload data with my organizational account Here’s the message error when I try to log with my organizational account:

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.')

Is there any way to fix it?

Thanks in advance

jasonrollins commented 4 years ago

Do you have any strange characters in your user name or password? The Office365 authentication user xml.sax.saxutils.escape() to clean up special characters, but maybe it is interfering?

jasonrollins commented 4 years ago

Do you have any special characters in your username and password?

On Wed, Apr 15, 2020 at 8:43 AM Odair Vaz notifications@github.com wrote:

Hello everyone, I'm trying to upload some files to SharePoint in Office 365 but the authentication fails even though that I put the right credentials.Here's the code.

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

get all the csv files

csv_files = list(filter(lambda x: '.csv' in x, os.listdir('./csvfiles'))) Office 365 Authentication

authcookie = Office365('https://tenant.sharepoint.com', username='login@tenant.com', password='password').GetCookies() site = Site('https://tenant.sharepoint.com/sites/Folder', version=Version.v365, authcookie=authcookie) ` However, I do manage to log in and upload data with my personal account, but I can’t upload data with my organizational account Here’s the message error when I try to log with my organizational account:

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.')

Is there any way to fix it?

Thanks in advance

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jasonrollins/shareplum/issues/84, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOVTVRFFTXIOEX6N4MJ3R3RMW2YDANCNFSM4MISYCVQ .

vennamanand1 commented 4 years ago

I'm having the same issue, and no special characters are observed in my username and password. Can you please help?

jasonrollins commented 4 years ago

Can you try version 0.4.5 and see if that one works?

vennamanand1 commented 4 years ago

Tried with 0.4.5 and the error is still the same.

vennamanand1 commented 4 years ago

please help

jasonrollins commented 4 years ago

Try setting verify_ssl=False in your site object. Site(verify_ssl=False)

On Tue, Apr 28, 2020, 1:00 AM vennamanand1 notifications@github.com wrote:

please help

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jasonrollins/shareplum/issues/84#issuecomment-620398391, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOVTVT7TB4U33PTW6XQASDROZWJNANCNFSM4MISYCVQ .

odairvaz commented 4 years ago

Do you have any strange characters in your user name or password? The Office365 authentication user xml.sax.saxutils.escape() to clean up special characters, but maybe it is interfering?

no, my password and username doesn't have stange characters

odairvaz commented 4 years ago

I changed my code but i'm still having the same issue

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

#get all the csv files
csv_files = list(filter(lambda x: '.csv' in x, os.listdir('./csvfiles')))

# Office 365 Authentication
authcookie = Office365('https://tenant.sharepoint.com',
                       username='login@tenant.com', password='password').GetCookies()
site = Site('https://tenant.sharepoint.com/sites/Folder',
            version=Version.v365, authcookie=authcookie, verify_ssl=False)

But i keep having the same error

Traceback (most recent call last):
  File "rest-api.py", line 19, in <module>
    authcookie = Office365('https://tenant.sharepoint.com',
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\site-packages\shareplum\office365.py", line 87, in get_cookies
    sectoken = self.get_security_token(self.username, self.password)
  File "C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\site-packages\shareplum\office365.py", line 79, in get_security_token
    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.')
kmahanna commented 4 years ago

I'm having a similar issue (tried 0.5.0 and 0.4.5)

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

authcookie = Office365('https://abc.sharepoint.com', username=username, password=password).GetCookies()
site = Site('https://abc.sharepoint.com/sites/MySharePointSite/', version=Version.v365, authcookie=authcookie, verify_ssl=False)

with this error:

Exception: ('Error authenticating against Office 365. Was not able to find an error code. Here is the SOAP response from Office 365'

It appears it can't get a security token.

amanmajeed commented 4 years ago

I am also facing a similar issue.

The code I used.

from shareplum import Site
from shareplum import Office365
from shareplum.site import Version
authcookie = Office365('https://***.sharepoint.com/', username='***', password='***').GetCookies()
site = Site('https://***.sharepoint.com/sites/Folder/', version=Version.v2016, authcookie=authcookie)
folder = site.Folder('Shared Documents/ME/')
folder.get_file('xyz.xlsx')

And the error I am facing is.

File "***\shareplum\office365.py", line 80, in get_security_token
    raise Exception('Error authenticating against Office 365. Error from Office 365:', message[0].text)
Exception: ('Error authenticating against Office 365. Error from Office 365:', "AADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access ''.")
odairvaz commented 4 years ago

@jasonrollins can you help us with this issue, do you have any other suggestion ? thanks

kmahanna commented 4 years ago

I was able to use this work around to pull data from the file I needed with requests_ntlm. My goal was to download the original file, but this will have to do. Accessing SharePoint 2013...

from requests_ntlm import HttpNtlmAuth
import requests
from io import BytesIO

auth = HttpNtlmAuth(username = username,
                        password = password)

responseObject = requests.get(url, auth = auth)

file = BytesIO(responseObject.content)
df = pd.read_excel(file)
Pear1125 commented 4 years ago

I also had this error

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.')

@jasonrollins It would be great if you can help us here.

nmduarte commented 3 years ago

Hi all,

Facing this error too - 'Error authenticating against Office 365. Error from Office 365:', 'AADSTS50126: Error validating credentials due to invalid username or password.'

Credentials works just fine if I try to login directly into SP or if I use my company email. But using the service account email, I can't pass that error.

Any help would be appreciated.

jasonrollins commented 3 years ago

Apologies. I'm not really sure how to debug logins. Some people have issues with their sites and some don't. I only have access to a personal Office 365 sharepoint site so I'm limited by what I can test.

On Fri, Oct 2, 2020 at 2:55 PM Nuno Tavares notifications@github.com wrote:

Hi all,

Facing this error too - 'Error authenticating against Office 365. Error from Office 365:', 'AADSTS50126: Error validating credentials due to invalid username or password.'

Credentials works just fine if I try to login directly into SP or if I use my company email. But using the service account email, I can't pass that error.

Any help would be appreciated.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jasonrollins/shareplum/issues/84#issuecomment-702929449, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOVTVUTRB7RVNUDOHHREPLSIYV4RANCNFSM4MISYCVQ .

divy-works commented 3 years ago

@jasonrollins The issues seems to be because of multi-factor authentication(such as okta) implemented on Office 365. I believe you are not facing this issue since your Office 365 has single-point authentication.

BrandonMFong commented 3 years ago

So I guess the question is how do we account for multi-factor authentication?

divy-works commented 3 years ago

So I guess the question is how do we account for multi-factor authentication?

I vote for closing this issue. Multi-factor authentication has been created to prevent automated login. If someone wants to automate data storage in SharePoint then they should ask IT Team to create exception for the login account. Trying to solve multi-factor authentication could be out of scope of your work.

black-syphilis commented 2 years ago

Exception: ('Error authenticating against Office 365. Error from Office 365:', 'AADSTS50034: The user account {EmailHidden} does not exist in the gmail.com directory. To sign into this application, the account must be added to the directory.')

Same problem and I confirm there isn't multi-factor authentification

ghost commented 2 years ago

Exception: ('Error authenticating against Office 365. Error from Office 365:', 'AADSTS50034: The user account {EmailHidden} does not exist in the gmail.com directory. To sign into this application, the account must be added to the directory.')

Same problem and I confirm there isn't multi-factor authentification

Same issue to me.

Colin-Root commented 2 years ago

Hello, guys, anyone can solve this issue? I also facing this issue:

raise Exception('Error authenticating against Office 365. Error from Office 365:', message[0].text) Exception: ('Error authenticating against Office 365. Error from Office 365:', 'AADSTS90023: Invalid STS request.')

CodePidgy commented 2 years ago

So is there just no way to use SharePlum on a SharePoint site that requires MFA? Is there no way to call the prompt to ask the user to login using MFA?

xingfengf21 commented 2 years ago

This is my code. Who can solve this problem

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

Office 365 Authentication

authcookie = Office365('https://xxxx.sharepoint.com/', username='xxxx@fengx.onmicrosoft.com', password='xxxxxxxx').GetCookies()

print(authcookie) site = Site('https://xxxxx.sharepoint.com/sites/site2', version=Version.v365, authcookie=authcookie, verify_ssl=False)

dir = 'Shared Documents\test' f = site.Folder(dir)

for file in f.files: print('--------') print(file) d = f.get_file(file['Name']) print(d)

for folder in f.folders: print(folder)

bikashh1207 commented 2 years ago

I am getting the same error, anyone has any solution?

manoharkakumani commented 1 year ago

Even I'm facing Exception: ('Error authenticating against Office 365. Was not able to find an error code. Here is the SOAP response from Office 365.

is there any solution?

ranjith590 commented 1 year ago

same error for me too... but not progressive way on that...

shivshankarpandit commented 1 year ago

I am getting the same error pls hep me on this. Thanks

ramvoci commented 1 year ago

Stuck with the same issue and looking for a solution.. Thanks..

elohbeck87 commented 1 year ago

same issue here as well. IT said they will not remove MFA either. Has anyone found a different library?

todorpeshin commented 1 year ago

Its not MFA issue I did test with users with MFA on/OFF on different site/tenant and it pass but on my organization it failing.

elohbeck87 commented 1 year ago

I was able to get around the issue by using Office365-REST-Python-Client and creating a registered app with read-write permissions.

mysticaljhb36 commented 1 year ago

Amazing ! Could you share how you resolved the problem? I'm having similar issue due to multi-factor authentication. Thanks.

elohbeck87 commented 1 year ago

This works well for me:

# we first need to register an app on the sharepoint site
# the app needs to have read-write permissions on the site
# install Office365-REST-Python-Client on the cluster

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

# define sharepoint site url
baseurl = sp_url
basesite = '/sites/{}'.format(sp_site_name)
siteurl = baseurl + basesite 

# get client credentials from secrets
username = sp_app_client_id
password = sp_app_client_secret

# authenticate with sharepoint site
ctx_auth = AuthenticationContext(siteurl)
ctx_auth.acquire_token_for_app(username, password)
ctx = ClientContext(siteurl, ctx_auth)
mysticaljhb36 commented 1 year ago

xt(siteurl

Thanks a lot. This looks workable using the app auth (using service principal ID) rather than user auth method to bypass the MFA. :)