gbotha
gbotha

Reputation: 1246

How to retrieve a list of students in a teacher's class in Google Classroom for Android App?

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

Upvotes: 0

Views: 4632

Answers (1)

gbotha
gbotha

Reputation: 1246

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.

Upvotes: 2

Related Questions