管理課程邀請

Classroom 中的邀請資源代表使用者邀請使用者以特定課程角色加入課程。

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

  • 由 Classroom 指派的邀請 id
  • 所接收邀請的使用者 userId
  • 使用者受邀參加的課程 courseId
  • role 邀請使用者在課程中的課程角色

建立邀請

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

Java

classroom/snippets/src/main/java/CreateInvitation.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/GetInvitation.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/AcceptInvitation.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/DeleteInvitation.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;
}