要使用Graph API将本地文件上传到SharePoint,您需要执行以下步骤:
以下是一个使用Python和requests库上传文件到SharePoint的示例代码:
import requests
# 设置变量
site_url = "https://your-sharepoint-site-url"
relative_url = "/sites/your-site-name/Shared%20Documents"
file_path = "path/to/your/local/file.txt"
access_token = "your-access-token"
# 构建请求头
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "text/plain",
}
# 读取文件内容
with open(file_path, "rb") as file:
file_content = file.read()
# 构建请求URL
upload_url = f"{site_url}{relative_url}/new-file-name.txt"
# 发送请求
response = requests.post(upload_url, headers=headers, data=file_content)
# 检查响应
if response.status_code == 201:
print("文件上传成功")
else:
print(f"文件上传失败,状态码:{response.status_code}")
领取专属 10元无门槛券
手把手带您无忧上云