3

This is my first approach to Google Classroom API. I'm trying to create a script that list the Owner's email of every course.

I thought of doing it by modifying the Quickstart "listCourses()" example, but when trying to get the user profile with Classroom.UserProfiles.get(); I get a 403 PERMISSION_DENIED error.

I've checked that every scope listed has been included:

        "oauthScopes": [
            "https://www.googleapis.com/auth/classroom.courses",
            "https://www.googleapis.com/auth/classroom.profile.emails",
            "https://www.googleapis.com/auth/classroom.profile.photos",
            "https://www.googleapis.com/auth/classroom.rosters",
            "https://www.googleapis.com/auth/classroom.rosters.readonly"
          ]

And the user running the script is registered as Super Admin on GSuite.

Data access has been checked as allowing data to be shared aswell.

This is the code I'm using

        function listCourses() {
            var response = Classroom.Courses.list();
            var courses = response.courses;

            if (courses && courses.length > 0) {
                for (i = 0; i < courses.length; i++) {
                    var course = courses[i];
                    var owner = Classroom.UserProfiles.get(course.ownerId).emailAddress;

                    Logger.log('%s (%s) - o: %s - stat: %s', course.name, course.id, owner, course.courseState);

                }
            } else {

                Logger.log('No courses found.');
            }  

        }

As far as I get, that should get me the list of courses, their ID number, the owner's email and the course status.

But execution stops on the UserProfile.get() line and the program stops with a 403 error.

Does anyone know what's the issue and how to solve it? Thank you very much.

Cooper
  • 59,616
  • 6
  • 23
  • 54
A.Garcia
  • 45
  • 5
  • 1
    I tested your code and it works for me, I think what could be happening is that one or more of the class owners might be outside of the domain. If this was the case, trying to get their information with `Classroom.UserProfiles.get()`. You should take a look at [this documentation](https://developers.google.com/classroom/reference/Access.Errors) on this particular issue and test out the get method [here](https://developers.google.com/classroom/reference/rest/v1/userProfiles/get) to see if particular users fail. – AMolina Aug 28 '19 at 06:30
  • Thank you very much! Thanks to your reply I realized that it is indeed only certain user IDs that make the script stop. I suspect that some of the users were deleted by the previous admin without reassigning their courses to a new teacher first and that is what's causing the issue. Thanks a lot! :) – A.Garcia Aug 28 '19 at 09:13
  • No problem, I'll post the answer for documentation! – AMolina Aug 28 '19 at 09:31

1 Answers1

2

Posting for documentation.

What could be happening is that one or more of the class owners might be outside of the domain. If this was the case, trying to get their information with Classroom.UserProfiles.get(). You should take a look at this documentation on this particular issue and test out the get method here to see if particular users fail.

Apparently this was the case, OP confirmed that some users were indeed removed from the domain.

AMolina
  • 1,355
  • 1
  • 7
  • 17