我正在尝试将 zip 文件上传到 Team Google Drive,但该文件出现在我自己的 Google Drive 中,而不是 Team 的文件夹中。我拥有团队(共享)文件夹的权限,我可以在那里手动上传文件。我能够获得 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)
任何帮助深表感谢!