从 Sharepoint rest 下载文件时出现 400 错误 api
400 error when downloading file from sharepoint rest api
我正在尝试使用 Python 通过 REST API 从共享点下载文件。这是我的代码:
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
如果请求的 url 是“http://sharepoint/sites/publishing/sales/_api/web”,return 代码将为 200,但尝试使用 GetFileByServerRelativeUrl 时,它将 return 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 = '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()
我正在尝试使用 Python 通过 REST API 从共享点下载文件。这是我的代码:
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
如果请求的 url 是“http://sharepoint/sites/publishing/sales/_api/web”,return 代码将为 200,但尝试使用 GetFileByServerRelativeUrl 时,它将 return 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 = '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()