Can anyone help me list the students for a specific class using Google Classroom API. I have searched through the API documentation and I can't seem to find out how to do it.
I basically need to do this https://developers.google.com/classroom/reference/rest/v1/courses.students/list
Here is the code I am using. It works with other functions (e.g. $service->students->listCoursesStudents($course_id);):
function G_Course_Roster($course_id) {
$clientId = '111111111111111111';
$serviceAccountName = '[email protected]';
$delegatedAdmin = '[email protected]';
// service account p12 key file:
$keyFile = '/home/rootfolder/vendor/Google OAuth Login-1111111111.p12';
$appName = 'name';
$scopes = array(
'https://www.googleapis.com/auth/classroom.profile.emails',
'https://www.googleapis.com/auth/classroom.profile.photos',
'https://www.googleapis.com/auth/classroom.rosters.readonly'
);
$creds = new Google_Auth_AssertionCredentials(
$serviceAccountName,
$scopes,
file_get_contents($keyFile)
);
$creds->sub = $delegatedAdmin;
$client = new Google_Client();
$client->setApplicationName($appName);
$client->setClientId($clientId);
$client->setAssertionCredentials($creds);
$service = new Google_Service_Classroom($client);
$optParams = array(
'courseId' => $course_id
);
$response = $service->students->listCoursesStudents($course_id);
return $response;
}
Ok the fix for PHP is $response = $service->courses_students->listCoursesStudents($course_id);
I basically had to guess based on the post above from Mr.Rebot. If someone knows where the PHP documentation is for Google I would appreciate a link
function G_Course_Roster($course_id) {
$clientId = '111111111111111111';
$serviceAccountName = '[email protected]';
$delegatedAdmin = '[email protected]';
// service account p12 key file:
$keyFile = '/home/rootfolder/vendor/Google OAuth Login-1111111111.p12';
$appName = 'name';
$scopes = array(
'https://www.googleapis.com/auth/classroom.profile.emails',
'https://www.googleapis.com/auth/classroom.profile.photos',
'https://www.googleapis.com/auth/classroom.rosters.readonly'
);
$creds = new Google_Auth_AssertionCredentials(
$serviceAccountName,
$scopes,
file_get_contents($keyFile)
);
$creds->sub = $delegatedAdmin;
$client = new Google_Client();
$client->setApplicationName($appName);
$client->setClientId($clientId);
$client->setAssertionCredentials($creds);
$service = new Google_Service_Classroom($client);
$optParams = array(
'courseId' => $course_id
);
$response = $service->courses_students->listCoursesStudents($course_id);
return $response;
}
I've tried in the Apps Script and it is working properly. Here is the code snippet:
function listStudent(){
var optionalArgs = {
pageSize: 10
};
var response = Classroom.Courses.list(optionalArgs);
var courses = response.courses;
if (courses && courses.length > 0) {
for (i = 0; i < courses.length; i++) {
var course = courses[i];
Logger.log('%s (%s)', course.name, course.id);
var response2 = Classroom.Courses.Students.list(course.id)
var students = response2.students;
if(students && students.length > 0){
for (j = 0; j < students.length; j++){
var student = students[j];
Logger.log('%s (%s)', student.profile, student.userId);
}
}
}
} else {
Logger.log('No courses found.');
}
}
You can test it out, apologies if this is not in PHP code.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With