这是我尝试运行的示例代码。
import requests
api_endpoint = 'https://www.googleapis.com/upload/drive/v3/files?supportsAllDrives=true'
headers = {
'Authorization': f'Bearer {access_token}'
}
json={"name": "a.jpg", "mimeType": "application/vnd.google-apps.shortcut", "parents": [{"id": "folder_id"}], "shortcutDetails": {"targetId": "target_file_id"}}
res = requests.post(api_endpoint, headers=headers, json=json)
print(res.status_code)
print(res.text)
即使此代码也没有选择该名称,它只是创建一个包含 json 数据的文本文件。
https://www.googleapis.com/drive/v3/files
而不是https://www.googleapis.com/upload/drive/v3/files
。parents
的值必须为 ["folder_id"]
,而不是 [{"id": "folder_id"}]
。当这些要点反映在你的脚本中时,就会变成如下所示。
请设置您的访问令牌、父文件夹 ID 和目标文件 ID。
import requests
access_token = "###" # Please set your access token.
api_endpoint = 'https://www.googleapis.com/drive/v3/files?supportsAllDrives=true'
headers = {
'Authorization': f'Bearer {access_token}'
}
json = {"name": "a.jpg", "mimeType": "application/vnd.google-apps.shortcut", "parents": ["folder_id"], "shortcutDetails": {"targetId": "target_file_id"}}
res = requests.post(api_endpoint, headers=headers, json=json)
print(res.status_code)
print(res.text)
"target_file_id"
的文件夹中创建了"folder_id"
的快捷方式文件。