Skip to content Skip to sidebar Skip to footer

Python W/google Drive Api

I am trying to transfer a file to my google drive using the API and a service account. When I run it, I get a file ID, but the file doesn't actually show up in my drive, so I have

Solution 1:

From your sample script, I thought that Python Quickstart is easy for you to understand. So it uses Python Quickstart at (https://developers.google.com/drive/v3/web/quickstart/python).

  1. Please carry out Step 1 and Step 2 at Python Quickstart.

  2. Please modify the sample script at Python Quickstart as follows.

From :

1.

SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'

2.

results = service.files().list(
    pageSize=10,fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
ifnot items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print('{0} ({1})'.format(item['name'], item['id']))

To :

1.

SCOPES = 'https://www.googleapis.com/auth/drive.file'

2.

f = 'test.html'
mime = 'text/html'
body = {
    'name': f,
    'mimeType': mime
    # 'parents': ## folderID ##, # If you want to upload file under folder, please use this.
}
results = service.files().create(
    body=body,
    media_body=MediaFileUpload(f, mimetype=mime, resumable=True)
).execute()
print(results)

When the file was uploaded, following JSON can be obtained. And you can see the file on Google Drive.

{"mimeType":"text/html","name":"test.html","id":"#####","kind":"drive#file"}

Post a Comment for "Python W/google Drive Api"