我正在使用谷歌应用程序脚本为教室创建上传文档的作业。但是,有一个错误。
执行失败:收到无效的JSON有效负载。名字不明 “course_work.materials [0]”中的“share_mode”:找不到字段。无效 收到JSON有效负载。未知名称“id”at 'course_work.materials [0] .drive_file':找不到字段。无效的JSON 有效载荷。未知名称“标题”在 'course_work.materials [0] .drive_file':找不到字段。 (第2行, 文件“TEST”)[总运行时间0.061秒]
这是我的代码。我知道错误在materials
,但我不确定我做错了什么。
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);
}
答案 0 :(得分:3)
找出问题的根源。我已更新您的代码以使其正常工作。
请求:
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);
}
结果:
我们收到Invalid JSON payload received.
,因为请求的格式化是错误的。它比我想象的要复杂一点,这就是我尝试使用Try this API查看请求格式的原因,它确实帮助我解决了你的问题。
希望这有帮助。
答案 1 :(得分:2)
根据文档Drivefile
属性title
标记为只读。只需使用id
。
https://developers.google.com/classroom/reference/rest/v1/DriveFile
答案 2 :(得分:2)
可以发送以下ajax请求来创建分配。下面的代码是为Angular编写的,但它可以很容易地转换为jQuery脚本。您可以构建自己的courseWork对象,并将其作为数据传递给对象。 ajax请求查看完整的Object结构访问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);
});
}