Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 645
Dana Asbury Avatar asked Aug 08 '16 21:08

Dana Asbury


People also ask

What is NTLM domain?

What Is NTLM Used For? Windows New Technology LAN Manager (NTLM) is a suite of security protocols offered by Microsoft to authenticate users' identity and protect the integrity and confidentiality of their activity.

What is Requests NTLM?

requests-ntlmThis package allows for HTTP NTLM authentication using the requests library.

Can Python use Windows authentication?

Python library that provides NTLM support, including an authentication handler for urllib2. This library allows you to retrieve content from (usually corporate) servers protected with windows authentication (NTLM) using the python urllib2.


1 Answers

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

like image 117
Teazane Avatar answered Sep 22 '22 06:09

Teazane