Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use google drive api for python?

I have been searching for google drive api documentation but have not been able to find a good one. Where can I find the details of its modules and functions to upload, download, get the list of files, etc. ??

like image 792
Mridul Gupta Avatar asked Jul 17 '16 12:07

Mridul Gupta


2 Answers

Here is documentation: https://developers.google.com/drive/v3/web/about-sdk But I think you've already found it. There are 3 api's:

  1. For Android
  2. For IOS
  3. HTTP(For everything else)

If you are programming some PC software you shold use HTTP Rest API. You will send http requests to google server instead of calling functions or modules. Here's samples for python

Also there is library for python to use http api

like image 106
dc914337 Avatar answered Sep 28 '22 05:09

dc914337


The Drive API allows you to upload certain types of binary data, or media. The specific characteristics of the data you can upload are specified on the reference page for any method that supports media uploads:

  • Maximum upload file size: The maximum amount of data you can store with this method.
  • Accepted media MIME types: The types of binary data you can store using this method.

You can make upload requests in any of the following ways. Specify the method you are using with the uploadType request parameters - Simple Upload, Multipart upload and Resumable upload.

When creating a file in Google Drive, you can convert some types file into a Google Docs, Sheets or Slides document by specifying the mimeType property of the file. The following sample shows how to upload a CSV file as a spreadsheet:

file_metadata = {
  'name' : 'My Report',
  'mimeType' : 'application/vnd.google-apps.spreadsheet'
}
media = MediaFileUpload('files/report.csv',
                        mimetype='text/csv',
                        resumable=True)
file = drive_service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
print 'File ID: %s' % file.get('id')

For downloading files, the API allows you to download files that are stored in Google Drive. Also, you can download exported versions of Google Documents (Documents, Spreadsheets, Presentations, etc.) in formats that your app can handle. Drive also supports providing users direct access to a file via the URL in the webViewLink property.

To download files, you make an authorized HTTP GET request to the file's resource URL and include the query parameter alt=media. For example:

GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs

Here is an example of performing a file download with our Drive API client libraries.

file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print "Download %d%%." % int(status.progress() * 100)

Here is a Python Quickstart - simple Python command-line application that makes requests to the Drive API.

like image 22
abielita Avatar answered Sep 28 '22 06:09

abielita