Skip to content

Instantly share code, notes, and snippets.

@xxbinxx
Last active March 28, 2017 11:20
Show Gist options
  • Save xxbinxx/346d3c772156529389e383d628e3c836 to your computer and use it in GitHub Desktop.
Save xxbinxx/346d3c772156529389e383d628e3c836 to your computer and use it in GitHub Desktop.
Example usage for python wrapper 'pyDrive', can also be used with django.
#!/usr/bin/python
#-------------------------------------------------------------------------------
# DOCS : https://googledrive.github.io/PyDrive/docs/build/html/index.html
# GITHUB REPO : https://github.com/googledrive/PyDrive
# example usage for python wrapper 'pyDrive'
# This example will create a file named "Hello.txt" with content 'Hello World!'
# in the drive of authenticated user.
#-------------------------------------------------------------------------------
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
DEFAULT_SETTINGS = {
'client_config_backend': 'file',
'client_config_file': 'client_secrets.json', #google credential file path
'oauth_scope': ['https://www.googleapis.com/auth/drive'] #Reference: https://developers.google.com/drive/v3/web/about-auth
}
gauth = GoogleAuth()
#-------------------------------------------------------------------------------
# There are 2 methods available to get google auth credentials (Custom, preBuilt):
# 1 (Custom) - Open up the link in the browser by any means and authorize google
# account. A "code" will be returned after successfull authentication.
# use that "code" to authenticate google OAuth
#-------------------------------------------------------------------------------
auth_url = gauth.GetAuthUrl() # Create authentication url user needs to visit
# Implement your customized authentication flow below
code = "" # Use your own custom method like VisitLinkAndAcquireCode(auth_url) that
# visits the passed url and retrieve 'code'
gauth.Auth(code) # Authorize and build service from the code
#-------------------------------------------------------------------------------
# 2 (preBuilt) - This will automatically starts a local web server using 8080
# or 8090 and then open ups the authUrl for you.
#-------------------------------------------------------------------------------
gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.
# ===== Following steps are common for both methods ======
drive = GoogleDrive(gauth)
file1 = drive.CreateFile({'title': 'Hello.txt'}) # Create GoogleDriveFile instance with title 'Hello.txt'.
file1.SetContentString('Hello World!') # Set content of the file from given string.
file1.Upload() # Similarly you can do:
# file1.Trash() - Move file to trash.
# file1.UnTrash() - Move file out of trash.
# file1.Delete() - Permanently delete the file.
# etc
# Auto-iterate through all files that matches this query
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment