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

前端 未结 4 1558
深忆病人
深忆病人 2020-12-07 17:20

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,

相关标签:
4条回答
  • 2020-12-07 17:21

    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")
    
    0 讨论(0)
  • 2020-12-07 17:25

    You can also use the sharepoint module from PyPI, self described as "Module and command-line utility to get data out of SharePoint"

    0 讨论(0)
  • 2020-12-07 17:41

    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'))
    
    0 讨论(0)
  • 2020-12-07 17:45

    A 401 response is an authentication error...

    That leaves one of your three variables as incorrect: url, user, pass. Requests Authentication Docs

    Your url looks incomplete.

    0 讨论(0)
提交回复
热议问题