Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DataSolveProblems/e5bf5c2ee8a298d7764948f302d9147c to your computer and use it in GitHub Desktop.
Save DataSolveProblems/e5bf5c2ee8a298d7764948f302d9147c to your computer and use it in GitHub Desktop.
import pickle
import os
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
def Create_Service(client_secret_file, api_service_name, api_version, *scopes):
SCOPES = [scope for scope in scopes[0]]
print(SCOPES)
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, SCOPES)
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(api_service_name, 'service created successfully')
return service
except Exception as e:
print(e)
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment