Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. from __future__ import print_function
  2. import httplib2
  3. import os
  4.  
  5. from apiclient import discovery
  6. from oauth2client import client
  7. from oauth2client import tools
  8. from oauth2client.file import Storage
  9.  
  10. try:
  11.     import argparse
  12.     flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
  13. except ImportError:
  14.     flags = None
  15.  
  16. # If modifying these scopes, delete your previously saved credentials
  17. # at ~/.credentials/sheets.googleapis.com-python-quickstart.json
  18. SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
  19. CLIENT_SECRET_FILE = 'client_secret.json'
  20. APPLICATION_NAME = 'Google Sheets API Python Quickstart'
  21.  
  22.  
  23. def get_credentials():
  24.     """Gets valid user credentials from storage.
  25.  
  26.    If nothing has been stored, or if the stored credentials are invalid,
  27.    the OAuth2 flow is completed to obtain the new credentials.
  28.  
  29.    Returns:
  30.        Credentials, the obtained credential.
  31.    """
  32.     home_dir = os.path.expanduser('~')
  33.     credential_dir = os.path.join(home_dir, '.credentials')
  34.     if not os.path.exists(credential_dir):
  35.         os.makedirs(credential_dir)
  36.     credential_path = os.path.join(credential_dir,
  37.                                    'sheets.googleapis.com-python-quickstart.json')
  38.  
  39.     store = Storage(credential_path)
  40.     credentials = store.get()
  41.     if not credentials or credentials.invalid:
  42.         flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
  43.         flow.user_agent = APPLICATION_NAME
  44.         if flags:
  45.             credentials = tools.run_flow(flow, store, flags)
  46.         else: # Needed only for compatibility with Python 2.6
  47.             credentials = tools.run(flow, store)
  48.         print('Storing credentials to ' + credential_path)
  49.     return credentials
  50.  
  51. def main():
  52.     """Shows basic usage of the Sheets API.
  53.  
  54.    Creates a Sheets API service object and prints the names and majors of
  55.    students in a sample spreadsheet:
  56.    https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
  57.    """
  58.     credentials = get_credentials()
  59.     http = credentials.authorize(httplib2.Http())
  60.     discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
  61.                     'version=v4')
  62.     service = discovery.build('sheets', 'v4', http=http,
  63.                               discoveryServiceUrl=discoveryUrl)
  64.  
  65.     spreadsheetId = '1O3s6t84A5pmZePvTvsVwFqCv5u-jYsa-gUE9nZmn7_o'
  66.     rangeName = 'B2:B5'
  67.     result = service.spreadsheets().values().get(
  68.         spreadsheetId=spreadsheetId, range=rangeName).execute()
  69.     values = result.get('values', [])
  70.  
  71.     if not values:
  72.         print('No data found.')
  73.     else:
  74.         print('Comment:')
  75.         for row in values:
  76.             # Print columns A and E, which correspond to indices 0 and 4.
  77.             print(values)
  78.  
  79.    
  80.  
  81.  
  82. if __name__ == '__main__':
  83.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement