|
| 1 | +"""Copyright 2022 Google LLC |
| 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 | + http://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.""" |
| 14 | + |
| 15 | +# [START classroom_add_teacher] |
| 16 | +from __future__ import print_function |
| 17 | + |
| 18 | +import os |
| 19 | + |
| 20 | +from google.auth.transport.requests import Request |
| 21 | +from google.oauth2.credentials import Credentials |
| 22 | +from google_auth_oauthlib.flow import InstalledAppFlow |
| 23 | +from googleapiclient.discovery import build |
| 24 | +from googleapiclient.errors import HttpError |
| 25 | + |
| 26 | +SCOPES = ['https://www.googleapis.com/auth/classroom.coursework.students'] |
| 27 | + |
| 28 | + |
| 29 | +def classroom_add_student_new(course_id): |
| 30 | + """ |
| 31 | + Adds a student to a course, the teacher has access to. |
| 32 | + The file token.json stores the user's access and refresh tokens, and is |
| 33 | + created automatically when the authorization flow completes for the first |
| 34 | + time. |
| 35 | + Load pre-authorized user credentials from the environment. |
| 36 | + TODO(developer) - See https://developers.google.com/identity for |
| 37 | + guides on implementing OAuth2 for the application. |
| 38 | + """ |
| 39 | + |
| 40 | + creds = None |
| 41 | + # The file token.json stores the user's access and refresh tokens, and is |
| 42 | + # created automatically when the authorization flow completes for the first |
| 43 | + # time. |
| 44 | + if os.path.exists('token.json'): |
| 45 | + creds = Credentials.from_authorized_user_file('token.json', SCOPES) |
| 46 | + # If there are no (valid) credentials available, let the user log in. |
| 47 | + if not creds or not creds.valid: |
| 48 | + if creds and creds.expired and creds.refresh_token: |
| 49 | + creds.refresh(Request()) |
| 50 | + else: |
| 51 | + flow = InstalledAppFlow.from_client_secrets_file( |
| 52 | + 'credentials.json', SCOPES) |
| 53 | + creds = flow.run_local_server(port=0) |
| 54 | + # Save the credentials for the next run |
| 55 | + with open('token.json', 'w', encoding="utf8") as token: |
| 56 | + token.write(creds.to_json()) |
| 57 | + |
| 58 | + enrollment_code = 'abc-def' |
| 59 | + student = { |
| 60 | + |
| 61 | + } |
| 62 | + try: |
| 63 | + service = build('classroom', 'v1', credentials=creds) |
| 64 | + student = service.courses().students().create( |
| 65 | + courseId=course_id, |
| 66 | + enrollmentCode=enrollment_code, |
| 67 | + body=student).execute() |
| 68 | + print( |
| 69 | + '''User {%s} was enrolled as a student in |
| 70 | + the course with ID "{%s}"''' |
| 71 | + % (student.get('profile').get('name').get('fullName'), |
| 72 | + course_id)) |
| 73 | + return student |
| 74 | + except HttpError as error: |
| 75 | + print(error) |
| 76 | + return error |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + # Put the course_id of course for which student needs to be added. |
| 81 | + classroom_add_student_new(478800920837) |
| 82 | +# [END classroom_add_teacher] |
0 commit comments