Google Classroom - Programmatically create assignment

情到浓时终转凉″ 提交于 2019-12-23 02:57:25

问题


I'm using google apps script to create assignment 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);
}

回答1:


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:

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.




回答2:


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

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




回答3:


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


来源:https://stackoverflow.com/questions/43041535/google-classroom-programmatically-create-assignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!