【发布时间】:2020-05-25 15:08:01
【问题描述】:
我正在编写一个自动化脚本来将目录中的文件和子目录从 ubuntu 传输到 SharePoint。而且我能够从目录中传输文件,但在上传子目录时失败。
我的代码供参考
#importing required packages
import sys
import requests
from requests_ntlm import HttpNtlmAuth
from config import config
import os
#Reading the configuration variables
username=config['sp_user'].split("@")[0]
password=config['sp_password']
sharepoint_url=config['sp_base_path']
sp_folder_url=config['folderUrl']
path=config['path']
domain=config['domain']
domain_username=domain+'\\'+username
#Reading the files from Linux
files = next(os.walk(path))[2]
for i in range(0, len(files)):
filename = files[i]
requestUrl = sharepoint_url + '/_api/web/getfolderbyserverrelativeurl(\'' + sp_folder_url + '\')/Files/add(url=\'' + filename + '\',overwrite=true)'
with open( filename, 'rb') as file_input:
try:
headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 'application/json;odata=verbose'}
r = requests.post(sharepoint_url + "/_api/contextinfo",auth=HttpNtlmAuth(domain_username,password), headers=headers)
formDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue']
headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 'application/json;odata=verbose', 'x-requestdigest' : formDigestValue}
uploadResult = requests.post(requestUrl,auth=HttpNtlmAuth(domain_username,'password'), headers=headers, data=file_input.read())
except Exception as err:
print("Some error occurred: " + str(err))
我可以知道我需要什么来实现这一目标,请帮助我弥合我的要求之间的差距。您能否尝试生成我的代码。提前致谢
更新
现在上传文件时出现以下错误。
{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied. You do not have permission to perform this action or access this resource."}}}
【问题讨论】:
标签: python rest sharepoint ubuntu-16.04 file-transfer