Manage Course Invitations

An Invitation resource in Classroom represents an invitation for a user to join a course with a specific course role.

Each Invitation resource contains the following fields:

  • id of the invitation that is assigned by Classroom.
  • userId of the user that the invitation is sent to.
  • courseId of the course the user is being invited to.
  • role the course role that the invited user will have in the course.

Create an Invitation

Create an invitation so that a user can join a course with the specified role by calling the invitations.create() method. Include the Invitation resource in the request body and specify the courseId, userId, and role.

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;

Retrieve an Invitation

Retrieve a specific invitation by calling the invitations.get() method and specifying the id of the invitation.

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;

Accept an Invitation

Accepting an invitation to a course deletes the invitation and adds the user to the course with the role specified in the invitation. Accept an invitation by calling the invitations.accept() method and specifying the id of the invitation.

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;
}

Delete an Invitation

The only way to update an invitation is to delete it and create a new invitation. To delete the invitation, call the invitations.delete() method and specify the 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;
}