C. Lang
C. Lang

Reputation: 593

How can I get a list of published coursework from a class while authenticated as a student on google classroom API?

As stated in the question, I am trying to get a list of all published coursework from my classes. I have used googles "PHP quick start" page in the classroom API documentation (https://developers.google.com/classroom/quickstart/php) to get me started. I have managed to successfully get an authenticated API client working and I have been able to print out a list of my class names (as demonstrated in the aforementioned "PHP quick start" page).

I have tried two methods of getting a list of coursework.

  1. From a reference of a class (school class and object oriented class!), run the getCourseMaterialSets function.

    # Before the code you see here, $client is set to an authenticated google API client
    $service = new Google_Service_Classroom($client);
    
    $mycourses = $service->courses->listCourses();
    $courseId = $mycourses->getCourses()[1]->getId();
    
    # This always returns 0 because the array is empty - I have tried several courses that definitely have coursework set.
    echo count($mycourses->getCourses($courseId)[1]->getCourseMaterialSets());
    

    This always returns an empty array even though I am sure the class in question has coursework set (as the image below shows).This image shows the coursework that should have been returned.

  2. Using $service->courses_courseWork->listCoursesCourseWork($courseId) I should be able to access a list of course work. However, this is throwing an error that tells me "Request had insufficient authentication scopes." This is strange because students should be able to access published coursework and by default listCoursesCourseWork returns only published coursework. Here is the code:

    # Before the code you see here, $client is set to a valid student client
    $service = new Google_Service_Classroom($client);
    
    $mycourses = $service->courses->listCourses();
    $courseId = $mycourses->getCourses()[1]->getId();
    
    # This throws an error that states "Request had insufficient authentication scopes."
    echo count($service->courses_courseWork->listCoursesCourseWork($courseId));
    

    And this is the error:

    Fatal error: Uncaught Google_Service_Exception: { "error": { "code": 403, "message": "Request had insufficient authentication scopes.", "errors": [ { "message": "Insufficient Permission", "domain": "global", "reason": "insufficientPermissions" } ], "status": "PERMISSION_DENIED" } } in /myWorkingDirectory/vendor/google/apiclient/src/Google/Http/REST.php:118 Stack trace: #0 /myWorkingDirectory/vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 /myWorkingDirectory/vendor/google/apiclient/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /myWorkingDirectory/ in /myWorkingDirectory/vendor/google/apiclient/src/Google/Http/REST.php on line 118
    

To summarise, I can't find a way to get a list of published coursework from a class. Please keep in mind that I am doing this from a students account and so will have limited permissions (but I should have the permissions required to view published coursework from a class that I have joined).

Upvotes: 0

Views: 751

Answers (1)

jdp
jdp

Reputation: 3516

You must provide one or more of the required authorization scopes. If you're using the scope referenced in the quickstart, it is insufficient for the courses.courseWork.list API.

A list of available scopes can be found in Google_Service_Classroom, or you can use the URL itself.

// either of the following should work.
$client->setScopes([
    Google_Service_Classroom::CLASSROOM_COURSEWORK_ME,
    'https://www.googleapis.com/auth/classroom.coursework.me'
]);

Try to choose the fewest scopes possible in order to accomplish what you need.

Upvotes: 1

Related Questions