|
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 | +""" |
14 | 16 |
|
15 | 17 | # [START apps_script_api_execute] |
16 | 18 | from __future__ import print_function |
17 | 19 |
|
18 | | -from googleapiclient import errors |
| 20 | +import google.auth |
19 | 21 | 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 |
23 | 23 |
|
24 | 24 |
|
25 | 25 | def main(): |
26 | 26 | """Runs the sample. |
27 | 27 | """ |
28 | | - SCRIPT_ID = 'ENTER_YOUR_SCRIPT_ID_HERE' |
| 28 | + # pylint: disable=maybe-no-member |
| 29 | + script_id = '1VFBDoJFy6yb9z7-luOwRv3fCmeNOzILPnR4QVmR0bGJ7gQ3QMPpCW-yt' |
29 | 30 |
|
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() |
40 | 32 | service = build('script', 'v1', credentials=creds) |
41 | 33 |
|
42 | 34 | # Create an execution request object. |
43 | 35 | request = {"function": "getFoldersUnderRoot"} |
44 | 36 |
|
45 | 37 | try: |
46 | 38 | # 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() |
50 | 41 | if 'error' in response: |
51 | 42 | # The API executed, but the script returned an error. |
52 | | - |
53 | 43 | # Extract the first (and only) set of error details. The values of |
54 | 44 | # this object are the script's 'errorMessage' and 'errorType', and |
55 | | - # an list of stack trace elements. |
| 45 | + # a list of stack trace elements. |
56 | 46 | error = response['error']['details'][0] |
57 | | - print("Script error message: {0}".format(error['errorMessage'])) |
| 47 | + print(f"Script error message: {0}.{format(error['errorMessage'])}") |
58 | 48 |
|
59 | 49 | if 'scriptStackTraceElements' in error: |
60 | 50 | # There may not be a stacktrace if the script didn't start |
61 | 51 | # executing. |
62 | 52 | print("Script error stacktrace:") |
63 | 53 | 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'])}") |
66 | 56 | else: |
67 | 57 | # 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: |
73 | 63 | print('No folders returned!') |
74 | 64 | else: |
75 | 65 | 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)}") |
78 | 68 |
|
79 | | - except errors.HttpError as e: |
| 69 | + except HttpError as error: |
80 | 70 | # 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) |
82 | 73 |
|
83 | 74 |
|
84 | 75 | if __name__ == '__main__': |
|
0 commit comments