【问题标题】:Can't upload to Team google drive from python无法从 python 上传到 Team google drive
【发布时间】:2021-12-21 23:49:25
【问题描述】:

我正在尝试将 zip 文件上传到团队 Google 云端硬盘,但该文件出现在我自己的 Google 云端硬盘中,而不是团队的文件夹中。我拥有团队(共享)文件夹的权限,我可以在那里手动上传文件。我能够获得 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)

非常感谢任何帮助!

【问题讨论】:

    标签: python upload gdrive


    【解决方案1】:

    寻找解决方案:

    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 不应成为元数据的一部分!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多