Can't upload to Team google drive from python

92 views Asked by At

i'm trying to upload a zip file to Team Google Drive, but the file appears in my own Google drive, and not in the Team's folder. I have a permissions for the team's (shared) folder and I can upload files there manually. I'm able to obtain the access_token.This is my code:

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)

Any help is much appreciated!

1

There are 1 answers

0
user2504276 On

Find a solution:

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 should not be a part of metadata!