谷歌教室api学生名单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谷歌教室api学生名单相关的知识,希望对你有一定的参考价值。

任何人都可以帮助我使用Google Classroom API列出特定班级的学生。我搜索了API文档,我似乎无法找到如何做到这一点。

我基本上需要做这个https://developers.google.com/classroom/reference/rest/v1/courses.students/list

这是我正在使用的代码。它适用于其他功能(例如$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;

}
答案

我尝试过在Apps脚本中运行正常。这是代码片段:

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.');
  }

}

你可以测试它,如果这不是php代码,请道歉。

希望这可以帮助。

另一答案

好的PHP修复是$ response = $ service-> courses_students-> listCoursesStudents($ course_id);

我基本上不得不根据罗伯特先生的帖子猜测。如果有人知道PHP文档在哪里用于Google,我会很感激链接

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;

}

另一答案

这是我想出的代码。此代码在单独的选项卡中列出每个课程的学生。

限制似乎是由于某种原因,Classroom API不会返回超过30名学生。

    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));

  }
 }
}

以上是关于谷歌教室api学生名单的主要内容,如果未能解决你的问题,请参考以下文章

谷歌地图在Android的标签片段上显示为灰色

在标签片段android中添加谷歌地点选择器

使用谷歌课堂 api 将成绩返回给学生

谷歌地图片段显示空地图

如何防止学生脱离极域电子教室的控制

谷歌地图片段无法膨胀