从sharepoint rest api下载文件时出现400错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从sharepoint rest api下载文件时出现400错误相关的知识,希望对你有一定的参考价值。

我正在尝试使用Python通过REST API从sharepoint下载文件。这是我的代码:

import requests
from requests_ntlm import HttpNtlmAuth

req = requests.get("http://sharepoint/sites/publishing/sales/_api/web/getfilebyserverrelativeurl('DocumentsFolderdata_04202015.csv')",auth=HttpNtlmAuth('domain\username','password'))
print req.status_code

如果请求的网址是“http://sharepoint/sites/publishing/sales/_api/web”,则返回代码将为200 ok,但在尝试使用GetFileByServerRelativeUrl时,它将返回400。

答案

最后,我可以使用sharepoint library访问这些文件

文件路径如下:http://sharepoint/sites/publishing/sales/Sales_Distribution/Data/record.csv

请注意,如果您尝试访问文件夹下的文件,则必须指定/ sites /中的路径根...

from sharepoint import SharePointSite, basic_auth_opener
import urllib2
from ntlm import HTTPNtlmAuthHandler

user = 'domainusername'
password = "password"

server_url = "http://sharepoint/"
site_url = server_url + "sites/publishing/sales/"

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, site_url, user, password)
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)

opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)

site = SharePointSite(site_url, opener)

salesList = site.lists['Sales Distribution']

fileList = salesList.get_rows(folder='/sites/publishing/sales/Sales_Distribution/Data')

print "================================="

url = fileList[5].as_dict()['EncodedAbsUrl']
file = fileList[5].open()

content = urllib2.urlopen(url)
print content.read()

以上是关于从sharepoint rest api下载文件时出现400错误的主要内容,如果未能解决你的问题,请参考以下文章

Sharepoint 授权令牌在 REST api 中的使用

Sharepoint 2013 通过 REST API:尝试创建项目时禁止出现错误 403

当站点下仍然可用时,未找到 Sharepoint REST Api 列表

sharepoint rest api 创建文档库 文件夹

使用 rest api 在 SharePoint 组中添加用户不起作用

SharePoint 2013 REST API AJAX 更新工作流任务