生徒グループを使用すると、的を絞った課題や共同作業など、授業の質を高めるために生徒を特定のグループに分けることができます。Classroom API を使用して、管理者と教師に代わってコース内の生徒グループを作成、変更、読み取ります。
次の方法で、生徒グループの作成、更新、削除、読み取りを行うことができます。
次の方法で、生徒グループ内のメンバーを追加、削除、読み取ることもできます。
デベロッパー プレビュー プログラムで生徒グループのメソッドにアクセスする
プレビュー版の生徒グループのメソッドにアクセスするには、次の操作を行う必要があります。
- デベロッパー プレビュー プログラム(DPP)に参加します。
- プレビュー機能にアクセスするようにクライアント ライブラリを設定します。DPP のメソッドは、標準のクライアント ライブラリと標準の HTTP リクエストでは公開されません。DPP に参加すると、特定のプログラミング言語の生徒グループ クライアント ライブラリにアクセスしてダウンロードできます。プレビュー バージョンとラベルを使用して選択したクライアント ライブラリを設定する方法については、プレビュー版 API へのアクセスをご覧ください。
- API リクエストを行うときにプレビュー バージョンとプレビュー ラベルを設定します。生徒グループのメソッドの場合、プレビュー バージョンは
V1_20250630_PREVIEW
、プレビュー ラベルはDEVELOPER_PREVIEW
です。プレビュー バージョンの API リクエストの例については、生徒グループを管理するをご覧ください。
ライセンスと資格要件
コースで生徒グループを作成、変更、削除したり、生徒グループにメンバーを追加または削除したりするには、次の条件を満たしている必要があります。
- リクエストを行うユーザーは、コースの教師またはドメインの管理者である必要があります。
- リクエストを行うユーザーには、Google Workspace for Education Plus ライセンスが割り当てられている必要があります。
- コースの所有者には、Google Workspace for Education Plus ライセンスが割り当てられている必要があります。
生徒グループとそのメンバーの読み取り
コースの管理者と教師は、割り当てられているライセンスに関係なく、生徒グループのデータを読み取ることができます。つまり、コース内の管理者または教師に代わって ListStudentGroups
エンドポイントと ListStudentGroupMembers
エンドポイントへのリクエストが許可されます。
コード例の前提条件
このガイドでは、Python のコード例を示します。また、次のものがあることを前提としています。
- Google Cloud プロジェクト Python クイックスタートの手順に沿って設定できます。
- プロジェクトの OAuth 同意画面に次のスコープを追加しました。
https://www.googleapis.com/auth/classroom.rosters
- 読み取り専用エンドポイントの場合は
https://www.googleapis.com/auth/classroom.rosters.readonly
。
- 生徒グループを管理するコースの ID。コースのオーナーには Google Workspace for Education Plus ライセンスが必要です。
- Google Workspace for Education Plus ライセンスを持つ教師または管理者の認証情報へのアクセス。
Python クイックスタートを起点として使用した場合は、プレビュー メソッドにアクセスするように Classroom サービスを変更します。
Python
classroom_service = googleapiclient.discovery.build(
serviceName='classroom',
version='v1',
credentials=creds,
static_discovery=False,
discoveryServiceUrl='https://classroom.googleapis.com/$discovery/rest?labels=DEVELOPER_PREVIEW&key=API_KEY')
お客様が対象であるか確認する
Classroom API には、ユーザーが学生グループとそのメンバーを作成および変更できるかどうかを事前に判断するのに役立つ userProfiles.checkUserCapability
エンドポイントが用意されています。
userProfiles.checkUserCapability
エンドポイントは、ユーザーが特定の機能(生徒グループの変更など)を使用できるかどうかのみを評価します。コースの役割に関する情報は提供されません。たとえば、ユーザーが CREATE_STUDENT_GROUP
ケーパビリティを持っていても、コースの受講生である場合、CreateStudentGroup
エンドポイントへのリクエストは成功しません。
Python
def check_student_groups_update_capability(classroom_service, course_id):
"""Checks whether a user is able to create and modify student groups in a course."""
capability = classroom_service.userProfiles().checkUserCapability(
userId="me", # Can also be set to a different user's email address or ID
capability="CREATE_STUDENT_GROUP",
previewVersion="V1_20240930_PREVIEW" # Required while the method is in the DPP.
).execute()
if capability.get("allowed"): # Retrieve the `allowed` boolean from the response.
print("User is allowed to create and modify student groups.")
else:
print("User is not allowed to create and modify student groups.")
生徒グループを管理する
生徒グループは CreateStudentGroup
エンドポイントを使用して作成できます。
Python
def create_student_group(classroom_service, course_id):
body = {
"title": "Team Blue"
}
response = classroom_service.courses().studentGroups().create(
courseId=course_id,
body=body,
previewVersion="V1_20250630_PREVIEW",
).execute()
print(response)
レスポンスには、新しく作成された生徒グループの id
、courseId
、生徒グループ title
が含まれます。
生徒グループ id
を使用して、個々の生徒グループを更新または削除できます。
Python
def update_student_group(classroom_service, course_id, student_group_id):
body = {
"title": "Team Green"
}
response = classroom_service.courses().studentGroups().patch(
courseId=course_id,
id=student_group_id,
body=body,
updateMask="title",
previewVersion="V1_20250630_PREVIEW"
).execute()
print(response)
def delete_student_group(classroom_service, course_id, student_group_id):
response = classroom_service.courses().studentGroups().delete(
courseId=course_id,
id=student_group_id,
previewVersion="V1_20250630_PREVIEW",
).execute()
print(response)
コース内の生徒グループを取得するには、ListStudentGroups
エンドポイントを使用します。
Python
def list_student_groups(classroom_service, course_id):
results = classroom_service.courses().studentGroups().list(
courseId=course_id,
previewVersion="V1_20250630_PREVIEW"
).execute()
studentGroups = results.get("studentGroups")
生徒グループのメンバーを管理する
生徒グループが正常に作成されたら、メンバーを追加できます。
Python
def add_student_group_member(classroom_service, course_id, student_group_id):
body = {
"userId": "student@schooldomain.com"
}
response = classroom_service.courses().studentGroups().studentGroupMembers().create(
courseId=course_id,
studentGroupId=student_group_id,
body=body,
previewVersion="V1_20250630_PREVIEW"
).execute()
print(response)
生徒グループからメンバーを削除する場合は、次のようなリクエストを行います。
Python
def delete_student_group_member(classroom_service, course_id, student_group_id):
response = classroom_service.courses().studentGroups().studentGroupMembers().delete(
courseId=course_id,
studentGroupId=student_group_id,
userId="student@schooldomain.com",
previewVersion="V1_20250630_PREVIEW"
).execute()
print(response)
グループ内のメンバーを読み取るには、次のリクエストを行います。
Python
def list_student_group_members(classroom_service, course_id, student_group_id):
results = classroom_service.courses().studentGroups().studentGroupMembers().list(
courseId=course_id,
studentGroupId=student_group_id,
previewVersion="V1_20250630_PREVIEW"
).execute()
print(results.get("studentGroupMembers"))
各 StudentGroupMember
リソースには、グループ メンバーの courseId
、studentGroupId
、userId
が含まれます。