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 badges3 Answers
Reset to default 2The 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"]
})