Advertisement
Guest User

Untitled

a guest
May 17th, 2017
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. from io import BytesIO, FileIO
  4.  
  5. import httplib2
  6. import os
  7.  
  8. from apiclient import discovery
  9. from googleapiclient.http import MediaIoBaseDownload
  10. from oauth2client import client
  11. from oauth2client import tools
  12. from oauth2client.file import Storage
  13.  
  14. try:
  15.     import argparse
  16.  
  17.     flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
  18. except ImportError:
  19.     flags = None
  20.  
  21. # If modifying these scopes, delete your previously saved credentials
  22. # at ~/.credentials/drive-python-quickstart.json
  23. SCOPES = 'https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.readonly'
  24. CLIENT_SECRET_FILE = 'client_secret.json'
  25. APPLICATION_NAME = 'Drive API Python Quickstart'
  26.  
  27.  
  28. def get_credentials():
  29.     """Gets valid user credentials from storage.
  30.  
  31.    If nothing has been stored, or if the stored credentials are invalid,
  32.    the OAuth2 flow is completed to obtain the new credentials.
  33.  
  34.    Returns:
  35.        Credentials, the obtained credential.
  36.    """
  37.     home_dir = os.path.expanduser('~')
  38.     credential_dir = os.path.join(home_dir, '.credentials')
  39.     if not os.path.exists(credential_dir):
  40.         os.makedirs(credential_dir)
  41.     credential_path = os.path.join(credential_dir,
  42.                                    'drive-python-quickstart.json')
  43.  
  44.     store = Storage(credential_path)
  45.     credentials = store.get()
  46.     if not credentials or credentials.invalid:
  47.         flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
  48.         flow.user_agent = APPLICATION_NAME
  49.         if flags:
  50.             credentials = tools.run_flow(flow, store, flags)
  51.         else:  # Needed only for compatibility with Python 2.6
  52.             credentials = tools.run(flow, store)
  53.         print('Storing credentials to ' + credential_path)
  54.     return credentials
  55.  
  56.  
  57. def main():
  58.     """Shows basic usage of the Google Drive API.
  59.  
  60.    Creates a Google Drive API service object and outputs the names and IDs
  61.    for up to 10 files.
  62.    """
  63.     credentials = get_credentials()
  64.     http = credentials.authorize(httplib2.Http())
  65.     service = discovery.build('drive', 'v3', http=http)
  66.  
  67.     file_id = '123456abcde'
  68.  
  69.     request = service.files().export_media(fileId=file_id, mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  70.  
  71.     fh = FileIO('data.xls', 'wb')
  72.     downloader = MediaIoBaseDownload(fh, request)
  73.     done = False
  74.     while done is False:
  75.         status, done = downloader.next_chunk()
  76.         print("Download %d%%." % int(status.progress() * 100))
  77.  
  78.  
  79. if __name__ == '__main__':
  80.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement