如何通过 Python 中的 REST API 访问共享点站点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20945822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:36:59  来源:igfitidea点击:

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

pythonrestauthenticationsharepointsharepoint-2013

提问by Indradhanush Gupta

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

我在本地 VM 的 SharePoint 2013 中有以下站点:

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

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:

当我从浏览器访问它时,它会提示我输入用户名和密码,然后工作正常。但是我正在尝试使用 Python 中的 REST API 来做同样的事情。我正在使用请求库,这就是我所做的:

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?

但是我得到一个 401。我不明白。我错过了什么?

Note: I followed this article http://tech.bool.se/using-python-to-request-data-from-sharepoint-via-rest/

注意:我按照这篇文章http://tech.bool.se/using-python-to-request-data-from-sharepoint-via-rest/

采纳答案by TomL

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.

您的 SharePoint 网站可能使用不同的身份验证方案。您可以通过检查 Firebug 或 Chrome 开发人员工具中的网络流量来检查这一点。

Luckily, the requests library supports many authentication options: http://docs.python-requests.org/en/latest/user/authentication/

幸运的是,请求库支持许多身份验证选项: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-ntmlplugin, I was able to access the site using code similar to this:

例如,我需要访问的网络之一使用 NTLM 身份验证。安装requests-ntml插件后,我可以使用类似以下的代码访问该站点:

import requests
from requests_ntlm import HttpNtlmAuth

requests.get("http://sharepoint-site.com", auth=HttpNtlmAuth('DOMAIN\USERNAME','PASSWORD'))

回答by Justin Poehnelt

A 401 response is an authenticationerror...

401 响应是身份验证错误...

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

这会使您的三个变量之一不正确:url, user, pass请求认证文档

Your url looks incomplete.

您的网址看起来不完整。

回答by ndemou

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

您还可以使用PyPI 中的sharepoint模块,自我描述为“从 SharePoint 获取数据的模块和命令行实用程序”

回答by Mihir Thakkar

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

以下是从 Python 调用 SharePoint 2016 REST API 以创建网站的示例。

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")