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

Attempting to retrieve course materials using the code below reports "no Materials found" but

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"]
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744087278a4556411.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信