Indradhanush Gupta Indradhanush Gupta - 1 year ago 471
Python Question

How to access a sharepoint site via the REST API in Python?

I have the following site in SharePoint 2013 in my local VM:


When I access this from the browser, it prompts me for the username and password and then works fine. However I am trying to do the same using the REST API in Python. I am using the requests library, and this is what I have done:

import requests
from requests.auth import HTTPBasicAuth

USERNAME = "Administrator"

PASSWORD = "password"

response = requests.get("http://win-5a8pp4v402g/sharepoint_test/site_1/", auth=HTTPBasicAuth(USERNAME, PASSWORD))

print response.status_code

However I get a 401. I dont understand. What am I missing?

Note: I followed this article

Answer Source

It's possible that your SharePoint site uses a different authentication scheme. You can check this by inspecting the network traffic in Firebug or the Chrome Developer Tools.

Luckily, the requests library supports many authentication options:

Fore example, one of the networks I needed to access uses NTLM authentication. After installing the requests-ntml plugin, I was able to access the site using code similar to this:

import requests
from requests_ntlm import HttpNtlmAuth

requests.get("", auth=HttpNtlmAuth('DOMAIN\\USERNAME','PASSWORD'))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download