3

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 = 'name@vocal-affinity-11111111.iam.gserviceaccount.com';
    $delegatedAdmin = 'email@email.net';
    // 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;

}

3 Answers3

2

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 = 'name@vocal-affinity-11111111.iam.gserviceaccount.com';
$delegatedAdmin = 'email@email.net';
// 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 was also unable to find any docs that apply to this specifically. You would think a mighty, multibillion-dollar transnational corporation could come up with readable docs...you would be wrong. – NessBird Feb 19 '21 at 00:04
1

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.

Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
0

Here is the code I came up with. This code lists the students from each course on a separate tab in sheets.

The limitation seems to be that the Classroom API will not return more than 30 students for some reason.

    function listStudents() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var optionalArgs = {
    pageSize: 0
  }; 

  //var response = Classroom.Courses.list();
  var courses = Classroom.Courses.list(optionalArgs).courses;


 for (var c = 0; c < courses.length; c++) {
      var course = courses[c];
      var courseName = course.name;
      var courseId = course.id;
      var students = Classroom.Courses.Students.list(courseId).students;        

  ss.insertSheet(course.name); 
  ss.getRange("A1").setValue('Last Name');
  ss.getRange("B1").setValue('First Name');
  ss.getRange("C1").setValue('Email');
  ss.getRange("D1").setValue('ID');
  ss.getActiveSheet().getRange(1,1,1,4).setBackground('silver').setFontSize(12).setFontWeight('bold');
  ss.setFrozenRows(1);


  for( var i = 0; i<students.length; i++) {


     var student = students[i]; 
     var profile = student.profile;
     var firstName = profile.name.givenName;
     var lastName = profile.name.familyName;
     var email = profile.emailAddress;

     SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(i+2, 1).setValue(lastName);
     SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(i+2, 2).setValue(firstName);
     SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(i+2, 3).setValue(email);
     SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(i+2, 4).setValue(email.substring(1,6));

  }
 }
}