Search code examples
google-apps-scriptgoogle-classroom

Invalid JSON payload received error when creating assignment


I'm using Google Apps Scripts to create assignments with an uploaded document for a classroom. However, there's an error.

Execution failed: Invalid JSON payload received. Unknown name "share_mode" at 'course_work.materials[0]': Cannot find field. Invalid JSON payload received. Unknown name "id" at 'course_work.materials[0].drive_file': Cannot find field. Invalid JSON payload received. Unknown name "title" at 'course_work.materials[0].drive_file': Cannot find field. (line 2, file "TEST") [0.061 seconds total runtime]

Here's my code. I know the error is in materials but I'm not sure what I did wrongly.

function myFunction() {
  var exec = Classroom.Courses.CourseWork.create({
    title: "Test File",
    state: "DRAFT",
    materials: [
      {
        driveFile: {id: "1ENk55RMtApIydyPFe0uyuhmu6nSV4", title: "Test File"},
        shareMode: "STUDENT_COPY"
      }
      ],
    workType: "ASSIGNMENT"
  }, "3896298178");
  Logger.log(exec);
}

Solution

  • Found out the root of your issue. I've updated your codes to make it work.

    Request:

    function myFunction() {
      var ClassSource =  {
        title: "Test File",
        state: "DRAFT",
        materials: [
          {
            driveFile:{
            driveFile: {
              id: "fileID", 
              title: "Sample Document"
    
            },
            shareMode: "STUDENT_COPY"
            }
    
          }
          ],
        workType: "ASSIGNMENT"
      };
    
      Classroom.Courses.CourseWork.create(ClassSource, COURSEID)
      //Logger.log(exec);
    }
    

    Result:

    enter image description here

    We receive Invalid JSON payload received. because the formating of the request is wrong. Its a little bit more complicated than I thought, that is why I tried using Try this API to see the request format and it really helped me solve your issue.

    Hope this helps.