最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google classroom - App script API fails to return Course Materials - Stack Overflow

programmeradmin7浏览0评论

Attempting to retrieve course materials using the code below reports "no Materials found" but the Classroom has multiple entries as Posted and Draft. All scopes are in place and a similar call for courseWork is successful.

    function getMaterials() {
    ScriptApp.requireAllScopes(ScriptApp.AuthMode.FULL);

  var sourceCourseId = "696613911962"; // Replace with your course ID  // 759489185424  TUC Bullying & 962 is Study Skills

  // Get materials (all)
  try {
    var destinationMaterialsResponse = Classroom.Courses.Materials.list(sourceCourseId);

    Logger.log("Raw Materials Response: " + JSON.stringify(destinationMaterialsResponse)); // Log the entire response

    if (destinationMaterialsResponse) {
      if (destinationMaterialsResponse.materials) {
        Logger.log("Materials: " + JSON.stringify(destinationMaterialsResponse.materials));
      } else {
        Logger.log("Materials property is missing or empty.");
      }
    } else {
      Logger.log("Destination Materials Response is null or undefined.");
    }
  } catch (e) {
    Logger.log("Error getting materials: " + e);
  }
}

Have I made an error? For CourseWork I have found that the state has to be declared, though I believe that Materials should be returned regardless of state (PUBLISHED or DRAFT)

Attempting to retrieve course materials using the code below reports "no Materials found" but the Classroom has multiple entries as Posted and Draft. All scopes are in place and a similar call for courseWork is successful.

    function getMaterials() {
    ScriptApp.requireAllScopes(ScriptApp.AuthMode.FULL);

  var sourceCourseId = "696613911962"; // Replace with your course ID  // 759489185424  TUC Bullying & 962 is Study Skills

  // Get materials (all)
  try {
    var destinationMaterialsResponse = Classroom.Courses.Materials.list(sourceCourseId);

    Logger.log("Raw Materials Response: " + JSON.stringify(destinationMaterialsResponse)); // Log the entire response

    if (destinationMaterialsResponse) {
      if (destinationMaterialsResponse.materials) {
        Logger.log("Materials: " + JSON.stringify(destinationMaterialsResponse.materials));
      } else {
        Logger.log("Materials property is missing or empty.");
      }
    } else {
      Logger.log("Destination Materials Response is null or undefined.");
    }
  } catch (e) {
    Logger.log("Error getting materials: " + e);
  }
}

Have I made an error? For CourseWork I have found that the state has to be declared, though I believe that Materials should be returned regardless of state (PUBLISHED or DRAFT)

Share Improve this question asked Mar 27 at 13:03 Class AdminClass Admin 134 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

The autocomplete doesn't include Materials.

The Classroom API docs have Material type, but not a REST resource with such name. Try using CourseWorkMaterials instead.

Agreeing to Wicket:

There are no REST resource named Materials in Google Classroom API but instead using CourseWorkMaterials to retrieve course work materials.

To make this work change the Materials to CourseWorkMaterials and in order to have a response that returns all states such as PUBLISHED, DRAFT and DELETED we need to add a second parameter called optionalArgs and add the CourseWorkState object.

Optional arguments.

Returns a list of course work material that the requester is permitted to view. Course students may only view PUBLISHED course work material. Course teachers and domain administrators may view all course work material.

const getAllMaterials = Classroom.Courses.CourseWorkMaterials.list(courseId,  {
  courseWorkMaterialStates: ["PUBLISHED", "DRAFT", "DELETED"]
})

Reference:

CourseWorkState

Correcting the call to CourseWorkMaterials.list and using the optional arguments solved my issue

    const getAllMaterials = Classroom.Courses.CourseWorkMaterials.list(courseId,  {
      courseWorkMaterialStates: ["PUBLISHED", "DRAFT", "DELETED"]
})
发布评论

评论列表(0)

  1. 暂无评论