Search code examples
google-apps-scriptgoogle-classroom

Using App Script how can I invite students to a classroom


I can add students directly into a classroom with this simple script which works. However, I would like to invite students rather than add them. It seems that the method is invitations.create and I have been trying to get variations to work without success. Can anyone help with my error here?

Classroom.Courses.invitations.create  (  {
  "userId": student1.xxxxx.ac.uk,
  "courseId": 653541812880,
  "role": STUDENT
}

All advice appreciated!

I have tried the invitations snippet and variations. It mostly fails with TypeError: Cannot read properties of undefined (reading 'create')

Many thanks for the helpful answer but which has resulted in a different error.

GoogleJsonResponseException: API call to classroom.invitations.create failed with error: Requested entity was not found. I am not sure which entity this error refers to since both the userId and courseId are correct (and work with the create rather than the invitation option.

The documentation indicates 'enum' STUDENT. But I dont understand enum


Solution

  • When you want to use "Method: invitations.create" with Advanced Google services, please use it as Classroom.Invitations.create(resource: Classroom_v1.Classroom.V1.Schema.Invitation). This can be confirmed with the auto-completion of the script editor. Ref When your script is modified, it becomes as follows.

    Modified script:

    Before you use this script, please confirm whether Google Classroom API has already been enabled at Advanced Google services, again.

    var res = Classroom.Invitations.create({
      "userId": "###",
      "courseId": "###",
      "role": "STUDENT"
    });
    console.log(res);
    
    • When the values of userId and courseId are valid values for using "Method: invitations.create", the script works. And, the user can receive an email.

    Note:

    • In this modification, it supposes that your values of userId and courseId are valid values. Please be careful about this.

    Reference: