Google 课堂 - 以编程方式创建作业

Google Classroom - Programmatically create assignment

我正在使用 google 应用程序脚本为课堂创建包含已上传文档的作业。但是,出现错误。

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]

这是我的代码。我知道错误在 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);
}

根据文档 Drivefile 属性 title 被标记为只读。只需使用 id.

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

找出问题的根源。我已经更新了您的代码以使其正常工作。

要求:

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 来查看请求格式,它确实帮助我解决了你的问题。

希望这对您有所帮助。

可以发送以下 ajax 请求来创建作业。下面的代码是为 Angular 编写的,但它可以很容易地转换为 jQuery 脚本。您可以构建自己的 courseWork 对象,该对象作为 ajax 的 'data' 请求传递,以查看完整的对象结构访问 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);
    });
}