Skip to content

Instantly share code, notes, and snippets.

@DataSolveProblems
Created June 13, 2019 14:18
Show Gist options
  • Save DataSolveProblems/cf16d9333ce941737622c59c9c0abde7 to your computer and use it in GitHub Desktop.
Save DataSolveProblems/cf16d9333ce941737622c59c9c0abde7 to your computer and use it in GitHub Desktop.
import pickle
import os
from pprint import pprint as pp
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
import pandas as pd
CLIENT_SECRET_FILE = 'client_secret.json'
API_SERVICE_NAME = 'sheets'
API_VERSION = 'v4'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
cred = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
cred = pickle.load(token)
if not cred or not cred.valid:
if cred and cred.expired and cred.refresh_token:
cred.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPE)
cred = flow.run_local_server()
with open('token.pickle', 'wb') as token:
pickle.dump(cred, token)
try:
service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
print('Service created successfully')
print(service)
except Exception as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment