|
| 1 | +""" |
| 2 | +Copyright 2018 Google LLC |
| 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 | + http://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 | +
|
| 16 | +""" |
| 17 | + |
| 18 | +# [START classroom_list_course_aliases] |
| 19 | + |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +import google.auth |
| 23 | +from googleapiclient.discovery import build |
| 24 | +from googleapiclient.errors import HttpError |
| 25 | + |
| 26 | + |
| 27 | +def classroom_list_course_aliases(course_id): |
| 28 | + """ |
| 29 | + Prints the list of the aliases of a specified course the user has access to. |
| 30 | + Load pre-authorized user credentials from the environment. |
| 31 | + TODO(developer) - See https://developers.google.com/identity |
| 32 | + for guides on implementing OAuth2 for the application. |
| 33 | + """ |
| 34 | + |
| 35 | + creds, _ = google.auth.default() |
| 36 | + try: |
| 37 | + service = build('classroom', 'v1', credentials=creds) |
| 38 | + course_aliases = [] |
| 39 | + page_token = None |
| 40 | + |
| 41 | + while True: |
| 42 | + response = service.courses().aliases().list( |
| 43 | + pageToken=page_token, |
| 44 | + courseId=course_id).execute() |
| 45 | + course_aliases.extend(response.get('aliases', [])) |
| 46 | + page_token = response.get('nextPageToken', None) |
| 47 | + if not page_token: |
| 48 | + break |
| 49 | + |
| 50 | + if not course_aliases: |
| 51 | + print('No course aliases found.') |
| 52 | + |
| 53 | + print("Course aliases:") |
| 54 | + for course_alias in course_aliases: |
| 55 | + print(f"{course_alias.get('alias')}") |
| 56 | + return course_aliases |
| 57 | + except HttpError as error: |
| 58 | + print(f"An error occurred: {error}") |
| 59 | + return error |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == '__main__': |
| 63 | + classroom_list_course_aliases('course_id') |
| 64 | + |
| 65 | +# [END classroom_list_course_aliases] |
0 commit comments