Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

http://win-5a8pp4v402g/sharepoint_test/site_1/

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 http://tech.bool.se/using-python-to-request-data-from-sharepoint-via-rest/

like image 354
Indradhanush Gupta Avatar asked Jan 06 '14 08:01

Indradhanush Gupta


People also ask

How do I access SharePoint REST API?

Now, you can interact directly with SharePoint objects by using any technology that supports standard REST capabilities. To access SharePoint resources using REST, construct a RESTful HTTP request by using the OData standard, which corresponds to the desired client object model API.

How do I use REST API in SharePoint online?

The REST API is implemented as Data-centric web service based on the Open Data Protocol or OData. The way these web services work, use each resource in the system is addressable by a specific URL that you pass off to the server. Let us look at this in Internet Explorer in which SharePoint site is open.

Can SharePoint connect to API?

With SharePoint API, you can perform typical CRUD (Create, Read, Update, and Delete) operations against SharePoint entities, such as Lists and Sites, by building REST endpoints. The REST endpoints in the SharePoint API correspond to the types and members in the SharePoint Client Object Models.


2 Answers

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: http://docs.python-requests.org/en/latest/user/authentication/

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("http://sharepoint-site.com", auth=HttpNtlmAuth('DOMAIN\\USERNAME','PASSWORD')) 
like image 184
TomL Avatar answered Sep 21 '22 17:09

TomL


Here is an examples of SharePoint 2016 REST API call from Python to create a site.

import requests,json,urllib from requests_ntlm import HttpNtlmAuth  root_url = "https://sharepoint.mycompany.com" headers = {'accept': "application/json;odata=verbose","content-type": "application/json;odata=verbose"} ##"DOMAIN\username",password  auth = HttpNtlmAuth("MYCOMPANY"+"\\"+"UserName",'Password')   def getToken():     contextinfo_api = root_url+"/_api/contextinfo"     response = requests.post(contextinfo_api, auth=auth,headers=headers)     response =  json.loads(response.text)     digest_value = response['d']['GetContextWebInformation']['FormDigestValue']     return digest_value  def createSite(title,url,desc):     create_api = root_url+"/_api/web/webinfos/add"     payload = {'parameters': {             '__metadata':  {'type': 'SP.WebInfoCreationInformation' },             'Url': url,             'Title': title,             'Description': desc,             'Language':1033,             'WebTemplate':'STS#0',             'UseUniquePermissions':True}         }     response = requests.post(create_api, auth=auth,headers=headers,data=json.dumps(payload))     return json.loads(response.text)  headers['X-RequestDigest']=getToken() print createSite("Human Resources","hr","Sample Description") 
like image 38
Mihir Thakkar Avatar answered Sep 20 '22 17:09

Mihir Thakkar