问题
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