Created
May 3, 2020 13:41
-
-
Save billydh/74c7d76337727c87e03989297163d428 to your computer and use it in GitHub Desktop.
Python function to initialise drive and sheets API services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from googleapiclient import discovery | |
from httplib2 import Http | |
from oauth2client import file, client, tools | |
def get_api_services(): | |
# define credentials and client secret file paths | |
credentials_file_path = './credentials/credentials.json' | |
clientsecret_file_path = './credentials/client_secret.json' | |
# define scope | |
SCOPE = 'https://www.googleapis.com/auth/drive' | |
# define store | |
store = file.Storage(credentials_file_path) | |
credentials = store.get() | |
if not credentials or credentials.invalid: | |
flow = client.flow_from_clientsecrets(clientsecret_file_path, SCOPE) | |
credentials = tools.run_flow(flow, store) | |
# define API service | |
http = credentials.authorize(Http()) | |
drive = discovery.build('drive', 'v3', http=http) | |
sheets = discovery.build('sheets', 'v4', credentials=credentials) | |
return drive, sheets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment