可以使用Microsoft Graph API代替api.onedrive.com来访问OneDrive。 Microsoft Graph API是一个综合的API,可以访问多个Microsoft服务(包括OneDrive)的数据。下面是使用Microsoft Graph API上传文件到OneDrive的Python代码示例:
import requests
import json
# Set the API endpoint and access token
url = "https://graph.microsoft.com/v1.0/me/drive/root/children/test.txt/content"
access_token = "INSERT_ACCESS_TOKEN_HERE"
# Define the headers and data to be uploaded
headers = {
"Authorization": "Bearer " + access_token,
"Content-Type": "text/plain"
}
data = "Hello, World!"
# Send the POST request to upload the file
response = requests.post(url, headers=headers, data=data)
# Print the response from the server
print(json.loads(response.content))
在这个示例中,我们定义了Microsoft Graph API的端点url (上传文件到root目录下的test.txt文件)和访问令牌access_token。 然后我们设置了要上传的文件的headers和data。 最后,我们使用requests库中的POST方法发送请求并解析响应。