学生和教师是用户个人资料与课程之间的具体对应关系,代表相应用户在课程中的角色。学生和教师并不是全局适用的:一个用户可以被指定为一门课程的教师,而另一名学生被指定为学生。“学生”或“教师”表示特定课程中特定用户的一组权限。
- 学生
- 学生资源表示已注册为特定课程的学生的用户。学生可以查看课程详细信息和该课程的教师。
- 教师
- 教师资源代表讲授特定课程的用户。教师可以查看和更改课程详细信息、查看教师和学生,以及管理其他教师和学生。
学生和教师由用户的唯一 ID 或电子邮件地址(由 Google Admin SDK 返回)标识。当前用户还可以使用 "me"
简写形式来引用自己的 ID。
直接添加
网域管理员可以绕过邀请流程,直接将网域中的用户作为教师或学生添加到网域中的课程。如果课程所有者在管理员的网域中,则课程会被视为在管理员的网域内。对于经过身份验证的网域管理员网域之外的用户或课程,应用必须通过使用 invitations.create()
方法发送邀请征得用户的同意。
添加或移除教师
网域管理员可以直接将网域内的教师添加到使用 teachers.create()
的课程,如以下示例所示:
.NET
using Google.Apis.Auth.OAuth2; using Google.Apis.Classroom.v1; using Google.Apis.Classroom.v1.Data; using Google.Apis.Services; using System; using System.Net; using Google; namespace ClassroomSnippets { // Class to demonstrate the use of Classroom Create Teacher API public class AddTeacher { /// <summary> /// Add teacher to the Course /// </summary> /// <param name="courseId"></param> /// <param name="teacherEmail"></param> /// <returns></returns> public static Teacher ClassroomAddTeacher( string courseId, string teacherEmail) { try { /* Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for your application. */ GoogleCredential credential = GoogleCredential.GetApplicationDefault() .CreateScoped(ClassroomService.Scope.ClassroomRosters); // Create Classroom API service. var service = new ClassroomService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "Classroom API Snippet" }); var teacher = new Teacher { UserId = teacherEmail }; // Add the teacher to the course. teacher = service.Courses.Teachers.Create(teacher, courseId).Execute(); Console.WriteLine( "User '{0}' was added as a teacher to the course with ID '{1}'.\n", teacher.Profile.Name.FullName, courseId); return teacher; } catch (Exception e) { // TODO(developer) - handle error appropriately if (e is AggregateException) { Console.WriteLine("Credential Not found"); } else if (e is GoogleApiException) { Console.WriteLine("Failed to Add the teacher. Error message: {0}", e.Message); } else { throw; } } return null; } } }
Java
import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Teacher; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; /* Class to demonstrate the use of Classroom Add Teacher API */ public class AddTeacher { /** * Add teacher to a specific course. * * @param courseId - Id of the course. * @param teacherEmail - Email address of the teacher. * @return newly created teacher * @throws IOException - if credentials file not found. */ public static Teacher addTeacher(String courseId, String teacherEmail) throws IOException { /* Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for your application. */ GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_ROSTERS)); HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); // Create the classroom API client Classroom service = new Classroom.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) .setApplicationName("Classroom samples") .build(); Teacher teacher = new Teacher().setUserId(teacherEmail); try { // Add a teacher to a specified course teacher = service.courses().teachers().create(courseId, teacher).execute(); // Prints the course id with the teacher name System.out.printf("User '%s' was added as a teacher to the course with ID '%s'.\n", teacher.getProfile().getName().getFullName(), courseId); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 409) { System.out.printf("User '%s' is already a member of this course.\n", teacherEmail); } else if (error.getCode() == 403) { System.out.println("The caller does not have permission.\n"); } else { throw e; } } return teacher; } }
PHP
use Google\Client; use Google\Service\Classroom; use Google\Service\Classroom\Teacher; use Google\service\Exception; function addTeacher($courseId, $teacherEmail) { /* Load pre-authorized user credentials from the environment. TODO (developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for your application. */ $client = new Client(); $client->useApplicationDefaultCredentials(); $client->addScope("https://www.googleapis.com/auth/classroom.profile.photos"); $service = new Classroom($client); $teacher = new Teacher([ 'userId' => $teacherEmail ]); try { // calling create teacher $teacher = $service->courses_teachers->create($courseId, $teacher); printf("User '%s' was added as a teacher to the course with ID '%s'.\n", $teacher->profile->name->fullName, $courseId); } catch (Exception $e) { if ($e->getCode() == 409) { printf("User '%s' is already a member of this course.\n", $teacherEmail); } else { throw $e; } } return $teacher; }
Python
teacher_email = 'alice@example.edu' teacher = { 'userId': teacher_email } try: teachers = service.courses().teachers() teacher = teachers.create(courseId=course_id, body=teacher).execute() print('User %s was added as a teacher to the course with ID %s' % (teacher.get('profile').get('name').get('fullName'), course_id)) except errors.HttpError as error: print('User "{%s}" is already a member of this course.' % teacher_email)
如果您是代表经过身份验证的教师添加其他教师,则必须使用 invitations.create()
方法。
您可以使用 teachers.delete()
方法移除课程中的其他教师。此操作只会从课程中移除指定的教师,而不会影响其分配给其他课程或用户个人资料。
注册或移除学生
网域管理员可以使用 students.create()
方法直接在网域中添加学生,如以下示例所示:
.NET
using Google.Apis.Auth.OAuth2; using Google.Apis.Classroom.v1; using Google.Apis.Classroom.v1.Data; using Google.Apis.Services; using System; using System.Net; using Google; namespace ClassroomSnippets { // Class to demonstrate the use of Classroom Create Student API public class AddStudent { public static Student ClassroomAddStudent(string courseId, string enrollmentCode) { try { /* Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for your application. */ GoogleCredential credential = GoogleCredential.GetApplicationDefault() .CreateScoped(ClassroomService.Scope.ClassroomRosters); var service = new ClassroomService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "Classroom API .NET Quickstart" }); var student = new Student { UserId = "me" }; var request = service.Courses.Students.Create(student, courseId); request.EnrollmentCode = enrollmentCode; student = request.Execute(); Console.WriteLine( "User '{0}' was enrolled as a student in the course with ID '{1}'.\n", student.Profile.Name.FullName, courseId); } catch (Exception e) { // TODO(developer) - handle error appropriately if (e is AggregateException) { Console.WriteLine("Credential Not found"); } else if (e is GoogleApiException) { Console.WriteLine("Failed to Add the Student. Error message: {0}", e.Message); } else { throw; } } return null; } } }
Java
import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Student; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; /* Class to demonstrate the use of Classroom Add Student API */ public class AddStudent { /** * Add a student in a specified course. * * @param courseId - Id of the course. * @param enrollmentCode - Code of the course to enroll. * @return newly added student * @throws IOException - if credentials file not found. */ public static Student addStudent(String courseId, String enrollmentCode) throws IOException { /* Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for your application. */ GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_ROSTERS)); HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( credentials); // Create the classroom API client Classroom service = new Classroom.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) .setApplicationName("Classroom samples") .build(); Student student = new Student().setUserId("me"); try { // Enrolling a student to a specified course student = service.courses().students().create(courseId, student) .setEnrollmentCode(enrollmentCode) .execute(); // Prints the course id with the Student name System.out.printf("User '%s' was enrolled as a student in the course with ID '%s'.\n", student.getProfile().getName().getFullName(), courseId); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 409) { System.out.println("You are already a member of this course."); } else if (error.getCode() == 403) { System.out.println("The caller does not have permission.\n"); } else { throw e; } } return student; } }
PHP
use Google\Client; use Google\Service\Classroom; use Google\Service\Classroom\Student; use Google\Service\Exception; function enrollAsStudent($courseId,$enrollmentCode) { /* Load pre-authorized user credentials from the environment. TODO (developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for your application. */ $client = new Client(); $client->useApplicationDefaultCredentials(); $client->addScope("https://www.googleapis.com/auth/classroom.profile.emails"); $service = new Classroom($client); $student = new Student([ 'userId' => 'me' ]); $params = [ 'enrollmentCode' => $enrollmentCode ]; try { $student = $service->courses_students->create($courseId, $student, $params); printf("User '%s' was enrolled as a student in the course with ID '%s'.\n", $student->profile->name->fullName, $courseId); } catch (Exception $e) { if ($e->getCode() == 409) { print "You are already a member of this course.\n"; } else { throw $e; } } return $student; }
Python
enrollment_code = 'abcdef' student = { 'userId': 'me' } try: student = service.courses().students().create( courseId=course_id, enrollmentCode=enrollment_code, body=student).execute() print( '''User {%s} was enrolled as a student in the course with ID "{%s}"''' % (student.get('profile').get('name').get('fullName'), course_id)) except errors.HttpError as error: print('You are already a member of this course.')
如果您是代表经过身份验证的教师添加学生,则必须使用 invitations.create()
方法。
您可以使用 students.delete()
方法将学生从课程中移除。此操作只会从课程中移除指定的学生,并不会影响他们注册其他课程或他们的用户个人资料。
检索用户的课程
要检索学生或教师的课程列表,请使用 courses.list()
并提供相应的 studentId
或 teacherId
。
检索用户的个人资料
要检索删节的配置文件(包括用户的 ID 和名称),请使用请求用户的 ID、电子邮件地址或“me”调用 userProfiles.get()
。
如需检索 emailAddress
字段,您必须添加 classroom.profile.emails
范围。
返回的 ID 与包含匹配的 studentId
或 teacherId
的 Directory API 用户资源相对应。