0

I have followed the Google Classroom tutorial on retrieving a list of a teacher's classes in Google Classroom (code below, relevant part from example Android code provided by Google).

Now I would like to retrieve a list of students within a specific class, and the only helpful information on the Classroom site give info on REST calls which I really don't know much about yet.

Android code to Retrieve a list of a teachers Google Classroom Classes

private List<String> getDataFromApi() throws IOException {
    ListCoursesResponse response = mActivity.mService.courses().list()
        .setPageSize(10)
        .execute();
    List<Course> courses = response.getCourses();
    List<String> names = new ArrayList<String>();
    if (courses != null) {
        for (Course course : courses) {
            names.add(course.getName());
        }
    }
    return names;
}

Here's how I modified the above code to try and get the list of student names in the classes as well. However the API returns a message of NO_PERMISSION when executing this code, even though a teacher should have permission to view the student names in their own classes.

private List<String> getDataFromApi() throws IOException {
    ListCoursesResponse response =    mActivity.mService.courses().list()
            .setPageSize(10)
            .execute();
    List<Course> courses = response.getCourses();
    List<String> names = new ArrayList<String>();
    if (courses != null) {
        names.add(courses.size()+"");

        for (Course course : courses) {

            names.add(course.getName());
            names.add(course.getId());

            ListStudentsResponse studentsResponse = mActivity.mService.courses().students().list(course.getId())
                    .setPageSize(40)
                    .execute();

            List<Student> students = studentsResponse.getStudents();

            names.add(students.size()+"");

            for (Student student : students) {

                names.add(student.toString());
            }
        }
    }
    return names;
}
gbotha
  • 1,231
  • 17
  • 23
  • Your question is not clear, you want to use Google Class room API in your application or you are developing a sample application where you are creating teacher-student management system? – sam_k Jul 29 '15 at 01:18
  • I already have an Android app for teachers, and I would like to use the Classroom API to get a list of courses, and the student names in each course. The Classroom team has released some sample code which shows how to authenticate a user and get a list of the classes under the user id. It works great. Now I would like to know how to get a list of student names under a particular classid - this all for an Android app. – gbotha Jul 29 '15 at 02:54
  • If there is any API available then it should work, Which API are you using? Post here. – sam_k Jul 29 '15 at 03:49
  • I got it working similar to your code. I did have to add ClassroomScopes.CLASSROOM_ROSTERS_READONLY to the scopes when authorizing via OAuth2. If you were working from the quickstart, use something like in the MainActivity - private static final String[] SCOPES = { ClassroomScopes.CLASSROOM_ROSTERS_READONLY, ClassroomScopes.CLASSROOM_PROFILE_EMAILS }; – anar Aug 07 '15 at 04:26

1 Answers1

2

Woo hoo, looks like I have to post a question in order to finally figure out the answer myself. So, the second bit of code I posted actually does work. The problem was the initial Scope in the example application only requested COURSE access and not ROSTER access. Once I added the ROSTER Scope I could retrieve the student data. Now I just have to learn how to parse JSON data.

gbotha
  • 1,231
  • 17
  • 23
  • If you use the library, like it appears you are, then there is no need to parse JSON. – Eric Koleda Jul 29 '15 at 18:58
  • I couldn't find a way to just get the list of student names from a particular course id. I used List students = studentsResponse.getStudents(); and then student.toString() I couldn't find code to just get the student first and last name. There was only option for userid, or profile, so the toString() gave me the entire student object in JSON, and then I did manage to parse it to get the first and last name, and email. I would like to get it directly if possible. Any suggestions? – gbotha Jul 30 '15 at 12:42
  • 1
    student.getProfile().getName().getFullName(); student.getProfile().getEmailAddress(); – anar Aug 07 '15 at 04:32