Python – What specifically should the domain be for NTLM authentication when using python-requests library

ntlmpythonpython-requestsrestsharepoint

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.

Best Answer

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