Search code examples
pythonwindowstfsntlm

Automatic NTLM with python on Windows


How can I use automatic NTLM authentication from python on Windows?

I want to be able to access the TFS REST API from windows without hardcoding my password, the same as I do from the web browser (firefox's network.automatic-ntlm-auth.trusted-uris, for example).


Solution

  • I found this answer which works great for me because:

    1. I'm only going to run it from Windows, so portability isn't a problem
    2. The response is a simple json document, so no need to store an open session

    It's using the WinHTTP.WinHTTPRequest.5.1 COM object to handle authentication natively:

    import win32com.client
    URL = 'http://bigcorp/tfs/page.aspx'    
    COM_OBJ = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')
    COM_OBJ.SetAutoLogonPolicy(0)
    COM_OBJ.Open('GET', URL, False)
    COM_OBJ.Send()
    print(COM_OBJ.ResponseText)