Google 课堂插件现已面向开发者正式推出!如需了解详情,请参阅
插件文档。
管理课程邀请
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Google 课堂中的 Invitation
资源表示邀请用户加入具有特定课程角色(学生、教师或所有者)的课程。
每个 Invitation
资源都包含以下字段:
id
:邀请的 Classroom 分配标识符。
userId
:已受邀加入课程的用户的 ID。
courseId
:用户受邀加入的课程。
role
:受邀用户在课程中将拥有的课程角色。
创建邀请
invitations.create()
方法可用于邀请用户加入课程并为其分配特定角色。在请求正文中添加 Invitation
资源,并指定 courseId
、userId
和 role
。
检索邀请
通过调用 invitations.get()
方法并指定邀请的 id
来检索特定邀请。
接受邀请
接受邀请会删除邀请,并将受邀用户添加到课程中,并为其分配邀请中指定的角色。通过调用 invitations.accept()
方法并指定邀请的 id
来接受邀请。
删除邀请
更新邀请的唯一方法是删除现有邀请并创建新邀请。如需删除邀请,请调用 invitations.delete()
方法并指定 id
。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-01。
[null,null,["最后更新时间 (UTC):2025-08-01。"],[],[],null,["# Manage Course Invitations\n\nAn [`Invitation` resource](/workspace/classroom/reference/rest/v1/invitations) in Classroom represents an invitation\nfor a user to join a course with a specific [course role](/workspace/classroom/reference/rest/v1/invitations#courserole): student, teacher,\nor owner.\n\nEach `Invitation` resource contains the following fields:\n\n- `id`: Classroom-assigned identifier for the invitation.\n- `userId`: The ID of the user that has been invited to the course.\n- `courseId`: The course that the user is being invited to.\n- `role`: The [course role](/workspace/classroom/reference/rest/v1/invitations#courserole) that the invited user will have in the course.\n\nCreate an Invitation\n--------------------\n\nThe [`invitations.create()`](/workspace/classroom/reference/rest/v1/invitations/create) method can be used to invite a user to a course\nwith a specific role. Include the [`Invitation` resource](/workspace/classroom/reference/rest/v1/invitations) in the request body\nand specify the `courseId`, `userId`, and `role`. \n\n### Java\n\nclassroom/snippets/src/main/java/CreateInvitation.java \n[View on GitHub](https://github.com/googleworkspace/java-samples/blob/main/classroom/snippets/src/main/java/CreateInvitation.java) \n\n```java\nInvitation invitation = null;\ntry {\n /* Set the role the user is invited to have in the course. Possible values of CourseRole can be\n found here: https://developers.google.com/classroom/reference/rest/v1/invitations#courserole.*/\n Invitation content =\n new Invitation().setCourseId(courseId).setUserId(userId).setRole(\"TEACHER\");\n\n invitation = service.invitations().create(content).execute();\n\n System.out.printf(\n \"User (%s) has been invited to course (%s).\\n\",\n invitation.getUserId(), invitation.getCourseId());\n} catch (GoogleJsonResponseException e) {\n // TODO (developer) - handle error appropriately\n GoogleJsonError error = e.getDetails();\n if (error.getCode() == 404) {\n System.out.printf(\"The course or user does not exist.\\n\");\n }\n throw e;\n} catch (Exception e) {\n throw e;\n}\nreturn invitation;\n```\n\nRetrieve an Invitation\n----------------------\n\nRetrieve a specific invitation by calling the [`invitations.get()`](/workspace/classroom/reference/rest/v1/invitations/get) method\nand specifying the `id` of the invitation. \n\n### Java\n\nclassroom/snippets/src/main/java/GetInvitation.java \n[View on GitHub](https://github.com/googleworkspace/java-samples/blob/main/classroom/snippets/src/main/java/GetInvitation.java) \n\n```java\nInvitation invitation = null;\ntry {\n invitation = service.invitations().get(id).execute();\n System.out.printf(\n \"Invitation (%s) for user (%s) in course (%s) retrieved.\\n\",\n invitation.getId(), invitation.getUserId(), invitation.getCourseId());\n} catch (GoogleJsonResponseException e) {\n GoogleJsonError error = e.getDetails();\n if (error.getCode() == 404) {\n System.out.printf(\"The invitation id (%s) does not exist.\\n\", id);\n }\n throw e;\n} catch (Exception e) {\n throw e;\n}\nreturn invitation;\n```\n\nAccept an Invitation\n--------------------\n\nAccepting an invitation deletes the invitation and adds the invited\nuser to the course with the role specified in the invitation. Accept an\ninvitation by calling the [`invitations.accept()`](/workspace/classroom/reference/rest/v1/invitations/accept) method and specifying the\n`id` of the invitation. \n\n### Java\n\nclassroom/snippets/src/main/java/AcceptInvitation.java \n[View on GitHub](https://github.com/googleworkspace/java-samples/blob/main/classroom/snippets/src/main/java/AcceptInvitation.java) \n\n```java\ntry {\n service.invitations().accept(id).execute();\n System.out.printf(\"Invitation (%s) was accepted.\\n\", id);\n} catch (GoogleJsonResponseException e) {\n GoogleJsonError error = e.getDetails();\n if (error.getCode() == 404) {\n System.out.printf(\"The invitation id (%s) does not exist.\\n\", id);\n }\n throw e;\n} catch (Exception e) {\n throw e;\n}\n```\n\nDelete an Invitation\n--------------------\n\nThe only way to update an invitation is to delete it and create a new\ninvitation. To delete the invitation, call the [`invitations.delete()`](/workspace/classroom/reference/rest/v1/invitations/delete) method\nand specify the `id`. \n\n### Java\n\nclassroom/snippets/src/main/java/DeleteInvitation.java \n[View on GitHub](https://github.com/googleworkspace/java-samples/blob/main/classroom/snippets/src/main/java/DeleteInvitation.java) \n\n```java\ntry {\n service.invitations().delete(id).execute();\n System.out.printf(\"Invitation (%s) was deleted.\\n\", id);\n} catch (GoogleJsonResponseException e) {\n GoogleJsonError error = e.getDetails();\n if (error.getCode() == 404) {\n System.out.printf(\"The invitation id (%s) does not exist.\\n\", id);\n }\n throw e;\n} catch (Exception e) {\n throw e;\n}\n```"]]