Created
April 9, 2019 02:09
-
-
Save uneasyguy/9edbba65cac4f1fd9d5b28c3f28e5297 to your computer and use it in GitHub Desktop.
script to run google apps scripts via python
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 __future__ import print_function | |
import pickle | |
import os.path | |
from googleapiclient import errors | |
from googleapiclient.discovery import build | |
from google_auth_oauthlib.flow import InstalledAppFlow | |
from google.auth.transport.requests import Request | |
SCOPES = [''] | |
credentials_path = '' | |
script_id='' | |
gsheets_function_name = '' | |
def main(): | |
creds = None | |
if os.path.exists('token.pickle'): | |
with open('token.pickle', 'rb') as token: | |
creds = pickle.load(token) | |
if not creds or not creds.valid: | |
if creds and creds.expired and creds.refresh_token: | |
creds.refresh(Request()) | |
else: | |
flow = InstalledAppFlow.from_client_secrets_file( | |
credentials_path, SCOPES) | |
creds = flow.run_local_server() | |
with open('token.pickle', 'wb') as token: | |
pickle.dump(creds, token) | |
service = build('script', 'v1', credentials=creds) | |
try: | |
request = {"function": gsheets_function_name} | |
response = service.scripts().run(body=request,scriptId=script_id).execute() | |
except errors.HttpError as error: | |
print(error.content) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment