nasserahmed96

GoogleAuth

Jan 6th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. """
  2. This file contains the authentication required to manipulate Nasser's google sheets via python
  3. """
  4. from __future__ import print_function
  5. import pickle
  6. import os.path
  7. from googleapiclient.discovery import build
  8. from google_auth_oauthlib.flow import InstalledAppFlow
  9. from google.auth.transport.requests import Request
  10. import gspread
  11. from oauth2client.service_account import ServiceAccountCredentials
  12. import sys
  13. GOOGLE_SHEET_API_SCOPE = ['https://www.googleapis.com/auth/spreadsheets']
  14.  
  15. def sheetAuth():
  16.     """This function is used to authenticate and return the creds
  17.    """
  18.     creds = None
  19.     if os.path.exists('token.pickle'):
  20.         with open('token.pickle', 'rb') as token:
  21.             creds = pickle.load(token)
  22.     if not creds or not creds.valid:
  23.         if creds and creds.expired and creds.refresh_token:
  24.             print("The token file is expired, please delete it")
  25.             creds.refresh(Request())
  26.         else:
  27.             flow = InstalledAppFlow.from_client_secrets_file('credentials.json', GOOGLE_SHEET_API_SCOPE)
  28.             creds = flow.run_local_server(port = 8000)
  29.         with open('token.pickle', 'wb') as token:
  30.             pickle.dump(creds, token)
  31.     return creds
Add Comment
Please, Sign In to add comment