javascriptgoogle-apps-scriptgoogle-classroom

How can I add a student (@gmail.com) to a Classroom class created inside my organization via Apps Script?


I have admin permissions in an Google Workspace organization, and I want to invite an external user (@gmail.com) as a student in a Classroom class, using Classroom API inside Google Apps Script.

Currently I am using this code to invite any user:

Classroom.Invitations.create({
    "userId": email,
    "courseId": courseId,
    "role": "STUDENT"
  });

It works okay if the email belongs to my organization's domain, but if I try the same code with a @gmail.com account, the script returns:

GoogleJsonResponseException: API call to classroom.invitations.create failed with error: The caller does not have permission

I do can add it manually inside Google Classroom, but it doesn't work via Apps Script. It might be worth mentioning that the owner account of the Classroom is not my account, but another one inside my domain, and the script I'm using is inside a shared drive.

SOLVED (thanks to Saddles): For it to work, the admin account trying to invite the students must be enrolled as a teacher in the course.


Solution

  • After some testing on a Google Workspace account, to add a @gmail.com user to a classroom using Google Apps Script, ensure that the Who can join classes in your domain? setting in the Admin console is set to Any user.

    Afterwards, add the Super Admin as a co-teacher in Google Classroom since the error GoogleJsonResponseException: API call to classroom.invitations.create failed with error: The caller does not have permission shows up if not.

    REFERENCE