使用Python

时间:2017-04-10 22:42:33

标签: python python-2.7 google-drive-api drive

我正在寻找使用python将文件上传到Google云端硬盘的代码。目前我只有authenticacion的代码。

代码V1

    from oauth2client.service_account import ServiceAccountCredentials      
    from httplib2 import Http

    #Authentication
    scopes = 'https://www.googleapis.com/auth/drive.metadata.readonly'
    credentials = ServiceAccountCredentials.from_json_keyfile_name('config/key_file.json', scopes)
    http_auth = credentials.authorize(Http())      
    service = discovery.build('drive', 'v3', http=http_auth)

    folder_id = 'XXXYYYZZZ'

有可能吗?任何的想法?感谢

代码V2

    from apiclient import discovery
    from oauth2client.service_account import ServiceAccountCredentials
    from apiclient.http import MediaFileUpload
    from httplib2 import Http

    #Authentication
    scopes = 'https://www.googleapis.com/auth/drive.file'
    credentials = ServiceAccountCredentials.from_json_keyfile_name('config/key_file.json', scopes)
    http_auth = credentials.authorize(Http())      
    service = discovery.build('drive', 'v3', http=http_auth)

    folder_id = 'XXXYYYZZZ'

    f = 'test.html'
    mime = 'text/html'
    body = {
        'name': f,
        'mimeType': mime,
        'parents':   folder_id
    }
    results = service.files().create(
        body=body,
        media_body=MediaFileUpload(f, mimetype=mime, resumable=True)
    ).execute()

0 个答案:

没有答案