Python Forum
Authentication error when accessing Microsoft SharePoint
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Authentication error when accessing Microsoft SharePoint
#1
Hi all, I'm trying to upload a file from my computer to my Microsoft SharePoint site using a python script.
I have a SharePoint site created through free MS teams org.
But I got an "Exception error" in the "authcookie" line.

My site_name is like 'https://myorg.sharepoint.com' and base_path is like 'Myorg-channel' (ie https://myorg.sharepoint.com/sites/Myorg-channel)
And I'm using MS Team admin user email and password who has the access to this channel.
Code I tried-
import requests
from shareplum import Office365

username = '[email protected]'
password = 'adminPassword'
site_name = 'https://myorg.sharepoint.com'
base_path = 'Myorg-channel'

# Obtain auth cookie
authcookie = Office365(base_path, username=username, password=password).GetCookies()
Error message-
Error:
Exception: ('Error authenticating against Office 365. Was not able to find an error code. Here is the SOAP response from Office 365', b'<?xml version="1.0" encoding="utf-8"?><S:Envelope xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Header><wsa:Action S:mustUnderstand="1" wsu:Id="Action">http://schemas.xmlsoap.org/ws/2005/02/trust/RSTR/Issue</wsa:Action><wsa:To S:mustUnderstand="1" wsu:Id="To">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To><wsse:Security S:mustUnderstand="1"><wsu:Timestamp wsu:Id="TS" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><wsu:Created>2020-06-05T12:01:00.5972083Z</wsu:Created><wsu:Expires>2020-06-05T12:06:00.5972083Z</wsu:Expires></wsu:Timestamp></wsse:Security></S:Header><S:Body xmlns:S="http://www.w3.org/2003/05/soap-envelope"><wst:RequestSecurityTokenResponse xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust"><wsp:AppliesTo><wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Address>Myorg-channel</wsa:Address></wsa:EndpointReference></wsp:AppliesTo><psf:pp xmlns:psf="http://schemas.microsoft.com/Passport/SoapServices/SOAPFault"><psf:reqstatus>0x8004882c</psf:reqstatus><psf:errorstatus>0x80045b00</psf:errorstatus></psf:pp></wst:RequestSecurityTokenResponse></S:Body></S:Envelope>')
Appreciate it if someone can help to rectify this error.
Reply
#2
I am not sure if this will help, but my dealings with sharepoint has meant it has used NTLM as an authorisation protocol for which there is a python package on PyPi, you can also see if the connection is correct via postman package prior to attempting to replicate in python.
Regards
-------- *
“Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.”
Reply
#3
I tried requests_ntlm package, but it didn't work.

import requests
from requests_ntlm import HttpNtlmAuth

requests.get("https://myorg.sharepoint.com", auth=HttpNtlmAuth("myorg"+"\\"+'[email protected]','adminPassword'))
Error:
<Response [403]>
Also, I tried using Sharepoint package, but not getting the access to my SharePoint site

from sharepoint import SharePointSite, basic_auth_opener

server_url = "https://myorg.sharepoint.com/"
site_url = server_url + "sites/Myorg-channel/"


opener = basic_auth_opener(server_url, '[email protected]','adminPassword')

site = SharePointSite(site_url, opener)

for sp_list in site.lists:
    print(sp_list.id)
Error:
HTTPError: Forbidden
Reply
#4
Hi

OK, then i would suggest before you carry on with python you install postman - link is here - its basically a "REST" client if you haven't used but there is the ability in there to use the NTLM protocol so at least with your user / domain / password combo you can see if you can get it to work form there, this would at least prove it is able to accept you requests. NTLM tbh is pants! it takes 3 request response combinations to authenticate
1. request to ntlm (sharepoint in this case)
2. if authenticated then an authentication "token" is returned in the http headers
3. request is sent with this token and whatever the request body is asking sharepoint to do - fetch docs etc


You can set up the same in SOAPUI and see the aforementoned transations, but I prefer Postman
Once you have established that working then apply the similar to python, especially if the packages you are using are either complex or you can't ascertain what they are doing, it might give some pointers as to where the error is.

If you need additional help with the postman set up am happy to help
Regards
-------- *
“Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.”
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating sharepoint excel file odd results cubangt 1 756 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 773 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Microsoft text phone verifying account cito 2 947 Jul-21-2022, 12:16 PM
Last Post: cito
  REST API x.509 authentication crossover 1 2,356 Feb-15-2022, 12:21 AM
Last Post: snippsat
  error : "Microsoft Visual C++ 14.0 is required. " Even its installed Barak 4 3,924 Oct-13-2021, 10:39 PM
Last Post: Underscore
  SharePoint Online/365 authentication using .cer file mart79 0 1,982 Nov-08-2020, 03:31 PM
Last Post: mart79
  List index out of range error while accessing 2 lists in python K11 2 2,061 Sep-29-2020, 05:24 AM
Last Post: K11
  Creating Excel files compatible with microsoft access vkallavi 0 1,565 Sep-17-2020, 06:57 PM
Last Post: vkallavi
  Trying to install Pyquil, says I need Microsoft Visual C++ Groucho 10 4,826 Feb-18-2020, 05:46 PM
Last Post: snippsat
  2 Microsoft word Docx content comparison vintysaw 2 5,637 Oct-17-2019, 10:41 AM
Last Post: vintysaw

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020