10

I am currently trying to access a sharepoint's API via python and the requests library. After inspecting the request via firebug I determined that it was using NTLM authentication so I installed the requests_ntlm plugin but I'm still getting a 401 error.

I came across this post, How to access a sharepoint site via the REST API in Python?, where the solution to use NTLM auth is this:

requests.get("http://sharepoint-site.com", auth=HttpNtlmAuth('DOMAIN\\USERNAME','PASSWORD'))

I am just confused by what domain is supposed to reference. I assumed it would just be my site_url but that still doesn't work. I've tried formatting it with two forward slashes as shown on the thread but also as one backslash as referenced here: https://curl.haxx.se/mail/lib-2005-11/0086.html.

When using NTLM, you can set domain by prepending it to the user name and separating the domain and name with a forward (/) or backward slash (\). Like this: "domain/user:password" or "domain\user:password". Some HTTP servers (on Windows) support this style even for Basic authentication.

import requests
from requests_ntlm import HttpNtlmAuth

username = "user"
password = "pass"
site_url = "https://sharepoint.site.com/foo/"
r = requests.get(site_url, auth=HttpNtlmAuth(site_url + username, password)
print(r.status_code)

I just find it interesting that it gives an explicit sample url in the request.get, but just gives the arbitrary "DOMAIN" in the auth parameter. The same goes for the documentation for the request-ntlm library seen here: https://github.com/requests/requests-ntlm:

requests.get("http://ntlm_protected_site.com",auth=HttpNtlmAuth('domain\\username','password'))

I'm guessing I just have the syntax messed up for the user name but I'm not quite sure the problem.

Community
  • 1
  • 1
Dana Asbury
  • 185
  • 2
  • 2
  • 12
  • 1
    This is an ancient question. Do you still have this issue? If yes... A) Can you auth with the *current* user? If yes, try: `requests_negotiate_sspi.HttpNegotiateAuth()` with no args. It will default to current user + domain. B) If you must auth with different credentials, it is impossible to generically answer your question, as each NT/AD network is different. Find a username and password that you *know* works to access Sharepoint website in a web browser. Then check domain name. Try `requests_ntlm.HttpNtlmAuth(user, pass)` with and without domain name. – kevinarpe Oct 26 '22 at 08:39
  • First comment too long. At big companies, domain name will be something like: BANK, INSURANCE, GLOBAL, EUROPE, etc. It will not be the URL like your above example code. Ask your technical coworkers or desktop support. They will know the domain name. Also: Generally, there are /few/ domain names on an NT/AD network (but each is different). – kevinarpe Oct 26 '22 at 08:43

1 Answers1

7

Just as you can see there : http://blog.carg.io/listing-and-updating-a-sharepoint-list-in-python/ the "domain" parameter doesn't seem to be mandatory. In fact, I got a 401 error until I discarded it from my code. Just try : requests.get("http://sharepoint-site.com", auth=HttpNtlmAuth('USERNAME','PASSWORD')).

Teazane
  • 134
  • 2
  • 11