python3谷歌驱动器文件上传

时间:2018-08-17 10:46:44

标签: google-drive-api google-api-client

帮助。 我指的是教程指南和YouTube。 但是,我不知道如何解决以下错误。 quickstart.py示例也起作用。 SCOPE变量已正确更改。 drive_service中没有文件()。

Traceback (most recent call last):
  File "D:/driveQuickStart.py", line 40, in <module>
main()
  File "D:/driveQuickStart.py", line 36, in main
ss = drive_service.files().create(body=file_metadata,media_body=media,fields='id').execute()
  File "C:\Users\soort\AppData\Local\Programs\Python\Python36\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
  File "C:\Users\soort\AppData\Local\Programs\Python\Python36\lib\site-packages\googleapiclient\http.py", line 842, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=multipart returned "Insufficient Permission">

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from googleapiclient.http import MediaFileUpload

# If modifying these scopes, delete the file token.json.
SCOPES = 'https://www.googleapis.com/auth/drive'

def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    drive_service = build('drive', 'v3', http=creds.authorize(Http()))

    # Call the Drive v3 API
    results = drive_service.files().list(
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print('{0} ({1})'.format(item['name'], item['id']))

    #upload
    file_metadata = {'name': 'photo.jpg'}
    media = MediaFileUpload('photo.jpg',mimetype='image/jpeg')
    ss = drive_service.files().create(body=file_metadata,media_body=media,fields='id').execute()
    print('File ID: %s' % ss.get('id'))

if __name__ == '__main__':
    main()

0 个答案:

没有答案