我正在编写一个自动化脚本,以将目录中的文件和子目录从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."}}}