【问题标题】:Google Classroom - Programmatically create assignmentGoogle 课堂 - 以编程方式创建作业
【发布时间】:2017-03-27 08:35:39
【问题描述】:

我正在使用 google 应用程序脚本为教室创建带有上传文档的作业。但是,有一个错误。

执行失败:收到无效的 JSON 负载。未知名称 “course_work.materials[0]”中的“share_mode”:找不到字段。无效的 收到 JSON 有效负载。未知名称“id”在 “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);
}

【问题讨论】:

    标签: google-apps-script google-classroom


    【解决方案1】:

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

    请求:

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

    希望这会有所帮助。

    【讨论】:

    • 联合和驱动器资源都被命名为'driveFile'。似乎多余且令人困惑。
    • @SpencerEaston 我同意它的冗余和混乱。也许功能请求可以解决这种困惑。
    【解决方案2】:

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

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

    【讨论】:

    • 感谢您的回复!现在出现了这个错误,[17-03-28 13:45:37:048 HKT] 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. (line 2, file "TEST") [0.046 seconds total runtime]
    【解决方案3】:

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

    【讨论】:

    • 这可以在没有 Ajax 的情况下完成吗 - 不管是什么 - 只是普通的 Google Apps 脚本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    相关资源
    最近更新 更多