chopz
chopz

Reputation: 381

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

Upvotes: 1

Views: 1819

Answers (3)

Abdul Aleem Khan
Abdul Aleem Khan

Reputation: 83

Following ajax request can be sent to create the assignment. The code below was written for Angular but it can be easily converted to jQuery script. You can build your own courseWork object that is being passed as 'data' of ajax request to see the full Object structure visit CourseWork API

    $http({
        url: 'https://classroom.googleapis.com/v1/courses/'+courseId+'/courseWork?access_token='+$scope.session.access_token,
        method: 'POST',   
        data:{
            "title": title,
            "description": description,
            "state": "PUBLISHED",
            "workType": "ASSIGNMENT",
            "submissionModificationMode": "MODIFIABLE_UNTIL_TURNED_IN",
            "associatedWithDeveloper": true
        }
    }).then(function(response){
        console.log(response);
        if(response.status==200){

        }
    }, function(response){
        console.log(response);
    });
}

Upvotes: 2

Mr.Rebot
Mr.Rebot

Reputation: 6791

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.

Upvotes: 5

Spencer Easton
Spencer Easton

Reputation: 5782

Per the docs Drivefile property title is marked read only. Just use the id.

https://developers.google.com/classroom/reference/rest/v1/DriveFile

Upvotes: 2

Related Questions