Skip to content

Commit 1fb1d92

Browse files
Implemented Google default authentication in apps_script_api_execute region tag of apps_script API. (#310)
Co-authored-by: himanshupr2627 <himanshupr@google.com>
1 parent 39937d6 commit 1fb1d92

File tree

1 file changed

+36
-45
lines changed

1 file changed

+36
-45
lines changed

apps_script/execute/execute.py

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,75 @@
1-
# Copyright Google Inc.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# https://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
1+
"""
2+
Copyright Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
1416

1517
# [START apps_script_api_execute]
1618
from __future__ import print_function
1719

18-
from googleapiclient import errors
20+
import google.auth
1921
from googleapiclient.discovery import build
20-
from oauth2client import client
21-
from oauth2client import file as oauth_file
22-
from oauth2client import tools
22+
from googleapiclient.errors import HttpError
2323

2424

2525
def main():
2626
"""Runs the sample.
2727
"""
28-
SCRIPT_ID = 'ENTER_YOUR_SCRIPT_ID_HERE'
28+
# pylint: disable=maybe-no-member
29+
script_id = '1VFBDoJFy6yb9z7-luOwRv3fCmeNOzILPnR4QVmR0bGJ7gQ3QMPpCW-yt'
2930

30-
# Set up the Apps Script API
31-
SCOPES = [
32-
'https://www.googleapis.com/auth/script.scriptapp',
33-
'https://www.googleapis.com/auth/drive.readonly',
34-
]
35-
store = oauth_file.Storage('token.json')
36-
creds = store.get()
37-
if not creds or creds.invalid:
38-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
39-
creds = tools.run_flow(flow, store)
31+
creds, _ = google.auth.default()
4032
service = build('script', 'v1', credentials=creds)
4133

4234
# Create an execution request object.
4335
request = {"function": "getFoldersUnderRoot"}
4436

4537
try:
4638
# Make the API request.
47-
response = service.scripts().run(body=request,
48-
scriptId=SCRIPT_ID).execute()
49-
39+
response = service.scripts().run(scriptId=script_id,
40+
body=request).execute()
5041
if 'error' in response:
5142
# The API executed, but the script returned an error.
52-
5343
# Extract the first (and only) set of error details. The values of
5444
# this object are the script's 'errorMessage' and 'errorType', and
55-
# an list of stack trace elements.
45+
# a list of stack trace elements.
5646
error = response['error']['details'][0]
57-
print("Script error message: {0}".format(error['errorMessage']))
47+
print(f"Script error message: {0}.{format(error['errorMessage'])}")
5848

5949
if 'scriptStackTraceElements' in error:
6050
# There may not be a stacktrace if the script didn't start
6151
# executing.
6252
print("Script error stacktrace:")
6353
for trace in error['scriptStackTraceElements']:
64-
print("\t{0}: {1}".format(trace['function'],
65-
trace['lineNumber']))
54+
print(f"\t{0}: {1}."
55+
f"{format(trace['function'], trace['lineNumber'])}")
6656
else:
6757
# The structure of the result depends upon what the Apps Script
68-
# function returns. Here, the function returns an Apps Script Object
69-
# with String keys and values, and so the result is treated as a
70-
# Python dictionary (folderSet).
71-
folderSet = response['response'].get('result', {})
72-
if not folderSet:
58+
# function returns. Here, the function returns an Apps Script
59+
# Object with String keys and values, and so the result is
60+
# treated as a Python dictionary (folder_set).
61+
folder_set = response['response'].get('result', {})
62+
if not folder_set:
7363
print('No folders returned!')
7464
else:
7565
print('Folders under your root folder:')
76-
for (folderId, folder) in folderSet.items():
77-
print("\t{0} ({1})".format(folder, folderId))
66+
for (folder_id, folder) in folder_set.items():
67+
print(f"\t{0} ({1}).{format(folder, folder_id)}")
7868

79-
except errors.HttpError as e:
69+
except HttpError as error:
8070
# The API encountered a problem before the script started executing.
81-
print(e.content)
71+
print(f"An error occurred: {error}")
72+
print(error.content)
8273

8374

8475
if __name__ == '__main__':

0 commit comments

Comments
 (0)