0
votes

I'm trying to use Apps Script to change the owner of a Google Classroom course.

According to the documentation I can do it using .patch(). This is the relevant script:

   var body = {            
     ownerId: owner
   }
   var mask={
     mask:'ownerId'
   }
       var optArgs = (mask, body); 

    var course = {
      name: classname,
      courseState: state,
      description: desc,
      descriptionHeading: descHead,
      room: room,
      section: section
    };
       Classroom.Courses.patch(course, courseId,  optArgs )

When I run it to update the owner I get the following error message

Could not be updated Invalid JSON payload received. Unknown name "ownerId": Cannot bind query parameter. Field 'ownerId' could not be found in request message.

Just to demonstrate a contrast, when I run .update() instead:

Classroom.Courses.update(course, courseId)

...the rest of the course details are updated perfectly, however .update() doesn't allow you to change the owner; it has to be a .patch() to do that.

3

3 Answers

1
votes

Just had the same problem, the issue is the optArgs - you just want the mask in there, not your changed data.

var optArgs = { mask: 'ownerId' }; 

var course = {
  name: classname,
  courseState: state,
  description: desc,
  descriptionHeading: descHead,
  room: room,
  section: section,
  ownerId: owner 
};

Classroom.Courses.patch(course, courseId,  optArgs );
0
votes

Based on documentation, this is working as intended.

courses.update has no ownerId parameters, only id.

But if you check courses.patch it has the updateMask parameter which accepts ownerId as a valid field.

For detailed information on this service, see the reference documentation for the Classroom API.

0
votes

I just tried this a few moments ago and it worked. Take into account that, for this to work, the user must already be a teacher in the specific class, and then this will make that teacher the owner.

function changeTeacherOwner() { 
var course = { 'ownerId': "EMAIL OF TEACHER" };
Classroom.Courses.patch(course, "COURSE ID",  {'updateMask':'ownerId'}); 
}