从sharepoint rest api下载文件时出现400错误 [英] 400 error when downloading file from sharepoint rest api

查看:126
本文介绍了从sharepoint rest api下载文件时出现400错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to download files from sharepoint through REST API using Python. Here is my code:

import requests
from requests_ntlm import HttpNtlmAuth

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

如果请求的网址是http://sharepoint/sites/publishing/sales/_api/web" 返回码将是 200 ok,但是当尝试使用 GetFileByServerRelativeUrl 时,它将返回 400.

If the requested url is "http://sharepoint/sites/publishing/sales/_api/web" the return code will be 200 ok, but when trying with GetFileByServerRelativeUrl, it will return 400.

推荐答案

最后,我可以使用 共享点库

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

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

Please note that if you are trying to access files under a folder, you have to specify the path roots from /sites/...

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

user = 'domain\username'
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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆