管理課程邀請

Classroom 的「邀請資源」代表使用者加入具有特定課程角色的課程邀請。

每個邀請資源都包含下列欄位:

  • 透過 Classroom 指派的邀請中的 id
  • 接收邀請的使用者 userId
  • 使用者受邀加入的課程中的 courseId 個課程。
  • role:受邀使用者在課程中的課程角色

建立邀請

建立邀請,讓使用者可透過呼叫 invitations.create() 方法以加入指定角色的課程。在要求主體中加入邀請資源,並指定 courseIduserIdrole

Java

classroom/snippets/src/main/java/CreateInvite.java
Invitation invitation = null;
try {
  /* Set the role the user is invited to have in the course. Possible values of CourseRole can be
  found here: https://developers.google.com/classroom/reference/rest/v1/invitations#courserole.*/
  Invitation content =
      new Invitation().setCourseId(courseId).setUserId(userId).setRole("TEACHER");

  invitation = service.invitations().create(content).execute();

  System.out.printf(
      "User (%s) has been invited to course (%s).\n",
      invitation.getUserId(), invitation.getCourseId());
} catch (GoogleJsonResponseException e) {
  // TODO (developer) - handle error appropriately
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The course or user does not exist.\n");
  }
  throw e;
} catch (Exception e) {
  throw e;
}
return invitation;

擷取邀請

呼叫 invitations.get() 方法並指定邀請的 id,即可擷取特定邀請。

Java

classroom/snippets/src/main/java/GetInvite.java
Invitation invitation = null;
try {
  invitation = service.invitations().get(id).execute();
  System.out.printf(
      "Invitation (%s) for user (%s) in course (%s) retrieved.\n",
      invitation.getId(), invitation.getUserId(), invitation.getCourseId());
} catch (GoogleJsonResponseException e) {
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The invitation id (%s) does not exist.\n", id);
  }
  throw e;
} catch (Exception e) {
  throw e;
}
return invitation;

接受邀請

接受課程邀請後,系統會刪除邀請,並將使用者以邀請中指定的角色加入課程。如要接受邀請,請呼叫 invitations.accept() 方法並指定邀請的 id

Java

classroom/snippets/src/main/java/AcceptInvite.java
try {
  service.invitations().accept(id).execute();
  System.out.printf("Invitation (%s) was accepted.\n", id);
} catch (GoogleJsonResponseException e) {
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The invitation id (%s) does not exist.\n", id);
  }
  throw e;
} catch (Exception e) {
  throw e;
}

刪除邀請

更新邀請的唯一方法是刪除邀請並建立新邀請。如要刪除邀請,請呼叫 invitations.delete() 方法並指定 id

Java

classroom/snippets/src/main/java/DeleteInvite.java
try {
  service.invitations().delete(id).execute();
  System.out.printf("Invitation (%s) was deleted.\n", id);
} catch (GoogleJsonResponseException e) {
  GoogleJsonError error = e.getDetails();
  if (error.getCode() == 404) {
    System.out.printf("The invitation id (%s) does not exist.\n", id);
  }
  throw e;
} catch (Exception e) {
  throw e;
}