我正试图将一个zip文件上传到Team Google Drive,但该文件出现在我自己的Google Drive中,而不是团队的文件夹中。我有团队(共享(文件夹的权限,可以手动上传文件。我可以获得access_token。这是我的代码:
import json
import requests
def get_token():
oauth = 'https://www.googleapis.com/oauth2/v4/token' # Google API oauth url
headers = {'content-type': 'application/x-www-form-urlencoded'}
data = {
'grant_type': 'refresh_token',
'client_id': '{CLIENT_ID}',
'client_secret': '{CLIENT_SECRET}',
'refresh_token': '{REFRESH_TOKEN}',
}
token = requests.post(oauth, headers=headers, data=data)
_key = json.loads(token.text)
return _key['access_token']
# Upload files to google drive using access token
def upload_to_drive(files):
token_key = get_token()
headers = {"Authorization": "Bearer " + token_key}
upload = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
print(upload.text)
if __name__ == '__main__':
file = "my_folder.zip"
para = {
"name": file,
# "parents": [FOLDER_ID],
"parents": [{
"kind": "drive#file",
"driveId": "{TEAM_DRIVE_ID}",
"id": "{FOLDER_ID}",
"supportsAllDrives": True
}],
}
files_for_upload = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
'file': ('application/zip', open("./" + file, "rb"))
}
upload_to_drive(files_for_upload)
非常感谢您的帮助!
找到解决方案:
import json
import requests
def get_token():
oauth = 'https://www.googleapis.com/oauth2/v4/token' # Google API oauth url
headers = {'content-type': 'application/x-www-form-urlencoded'}
data = {
'grant_type': 'refresh_token',
'client_id': '{CL}',
'client_secret': '{CLIENT_SECRET}',
'refresh_token': '{REFRESH_TOKEN}',
}
token = requests.post(oauth, headers=headers, data=data)
_key = json.loads(token.text)
return _key['access_token']
# Upload files to google drive using access token
def upload_to_drive(files):
token_key = get_token()
headers = {"Authorization": "Bearer " + token_key}
upload = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&supportsAllDrives=true",
headers=headers,
files=files
)
print(upload.text)
if __name__ == '__main__':
file = "my_file.zip"
para = {
"name": file,
"parents": ['{FOLDER_ID}'],
}
files_for_upload = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
'file': ('application/zip', open("./" + file, "rb"))
}
upload_to_drive(files_for_upload)
supportsAllDrives不应该是元数据的一部分!