python 操作sharepoint 文件夹 带token
要使用Python操作SharePoint文件夹,你可以使用sharepy库。这个库可以帮助你通过提供的令牌(token)进行身份验证,并与SharePoint服务器进行交互。
首先,安装sharepy库。你可以使用以下命令在命令行中安装:
pip install sharepy
然后,使用以下代码示例来操作SharePoint文件夹:
import sharepy
# 定义SharePoint网站URL和令牌
site_url = "https://your-sharepoint-site-url"
token = "your-token"
# 创建SharePoint会话
s = sharepy.connect(site_url, token=token)
# 列出文件夹中的文件
response = s.get("https://your-sharepoint-site-url/_api/web/GetFolderByServerRelativeUrl('/path-to-folder')/Files")
files = response.json()
for file in files['d']['results']:
print(file['Name'])
# 上传文件到文件夹
file_path = '/path-to-file/file.txt'
with open(file_path, 'rb') as f:
file_name = file_path.split('/')[-1]
response = s.post("https://your-sharepoint-site-url/_api/web/GetFolderByServerRelativeUrl('/path-to-folder')/Files/add(url='{0}', overwrite=true)".format(file_name), data=f)
print(response)
# 下载文件
response = s.get("https://your-sharepoint-site-url/_api/web/GetFolderByServerRelativeUrl('/path-to-folder')/Files('file.txt')/$value")
with open('downloaded_file.txt', 'wb') as f:
f.write(response.content)
请注意,你需要将示例代码中的以下内容替换为你自己的信息:
https://your-sharepoint-site-url:你的SharePoint网站的URL。your-token:你的访问令牌。
还需要将/path-to-folder和/path-to-file/file.txt替换为你要操作的文件夹和文件的路径。
这是使用sharepy库操作SharePoint文件夹的简单示例。你可以根据自己的需求进行修改和扩展
原文地址: https://www.cveoy.top/t/topic/hMfQ 著作权归作者所有。请勿转载和采集!