Search code examples
google-apps-scriptgoogle-apigoogle-oauthgoogle-classroom

Google Classroom API - PERMISSION DENIED


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.


Solution

  • 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.