如何查询Microsoft Graph Explorer及用Python调用其接口修改SharePoint文件
问题解答
1. 关于Microsoft Graph Explorer的使用
Microsoft Graph Explorer是微软官方的在线API测试工具,直接在浏览器中打开即可使用:
- 登录你的微软工作/学校账号或个人微软账号
- 在顶部输入框中填写Graph API接口地址(比如你提到的
https://graph.microsoft.com/drive/root/children) - 选择请求方法(GET/POST/PUT等),配置请求头和参数
- 点击"Run query"发起调用,查看响应结果
- 可通过左侧面板选择所需权限,确保调用有足够的权限支持
2. Python调用Graph API获取Drive根目录文件列表
使用Client ID和Client Secret调用接口,需先通过客户端凭证流获取Access Token,再发起GET请求。
步骤:
- 安装依赖库:
pip install requests
- 编写脚本:
import requests # 替换为你的租户ID、Client ID、Client Secret tenant_id = "your-tenant-id" client_id = "your-client-id" client_secret = "your-client-secret" # 获取Access Token token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token" token_payload = { "grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret, "scope": "https://graph.microsoft.com/.default" } token_response = requests.post(token_url, data=token_payload) access_token = token_response.json().get("access_token") if not access_token: print("获取Access Token失败") exit() # 调用Drive根目录文件列表接口 api_url = "https://graph.microsoft.com/v1.0/drive/root/children" headers = { "Authorization": f"Bearer {access_token}" } response = requests.get(api_url, headers=headers) if response.status_code == 200: print("获取文件列表成功:") print(response.json()) else: print(f"请求失败,状态码:{response.status_code}") print(response.json())
注意:需确保你的应用已在Azure AD中配置Files.Read或Files.Read.All权限,且已获得管理员同意。
3. Python通过Graph API修改SharePoint文件
修改SharePoint文件分为修改内容或属性,以下以修改文件内容为例:
步骤:
- 确定目标文件的Graph API路径,格式为:
https://graph.microsoft.com/v1.0/sites/{site-id}/drive/items/{item-id}/content
site-id可通过Graph Explorer查询https://graph.microsoft.com/v1.0/sites获取item-id是目标文件的ID,可通过查询站点下的文件列表获取
- 编写修改脚本:
import requests # 替换为你的租户ID、Client ID、Client Secret、Site ID、Item ID tenant_id = "your-tenant-id" client_id = "your-client-id" client_secret = "your-client-secret" site_id = "your-site-id" item_id = "your-item-id" # 获取Access Token token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token" token_payload = { "grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret, "scope": "https://graph.microsoft.com/.default" } token_response = requests.post(token_url, data=token_payload) access_token = token_response.json().get("access_token") if not access_token: print("获取Access Token失败") exit() # 准备要写入的新内容 new_content = "这是修改后的文件内容" api_url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drive/items/{item_id}/content" headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "text/plain" # 根据文件类型调整,比如application/pdf、application/json等 } # 发起PUT请求修改文件内容 response = requests.put(api_url, headers=headers, data=new_content.encode("utf-8")) if response.status_code in [200, 201]: print("文件修改成功") else: print(f"修改失败,状态码:{response.status_code}") print(response.json())
注意:应用需配置Files.ReadWrite或Files.ReadWrite.All权限,并获得管理员同意。若修改文件属性(如文件名、描述),可调用PATCH请求到https://graph.microsoft.com/v1.0/sites/{site-id}/drive/items/{item-id},在请求体中传入要修改的属性。
内容的提问来源于stack exchange,提问作者Mitesh Khadgi
相关产品推荐
相关产品推荐

