在 google class 房间 API 中作为学生进行身份验证时,如何从 class 获取已发布的课程作业列表?

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

如问题中所述,我正在尝试从我的 classes 中获取所有已发布课程作业的列表。我在 classroom API 文档 (https://developers.google.com/classroom/quickstart/php) 中使用了 googles "PHP quick start" 页面来帮助我入门。我已经成功地让经过身份验证的 API 客户端正常工作,并且我已经能够打印出我的 class 名称列表(如上述 "PHP quick start" 页面所示)。

我尝试了两种获取课程作业列表的方法。

  1. 来自 class(学校 class 和面向对象 class!)的参考,运行 getCourseMaterialSets 函数。

    # 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());
    

    这总是 returns 一个空数组,即使我确定有问题的 class 已经设置了课程作业(如下图所示)。

  2. 使用 $service->courses_courseWork->listCoursesCourseWork($courseId) 我应该能够访问课程作业列表。但是,这引发了一个错误,告诉我 "Request had insufficient authentication scopes." 这很奇怪,因为学生应该能够访问已发布的课程作业,并且默认情况下 listCoursesCourseWork returns 只能访问已发布的课程作业。 这是代码:

    # 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));
    

    这是错误:

    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
    

总而言之,我找不到从 class 获取已发布课程作业列表的方法。请记住,我是通过学生帐户执行此操作的,因此权限有限(但我应该具有从我加入的 class 查看已发布课程作业所需的权限)。

您必须提供一项或多项要求 authorization scopes。如果您使用的是快速入门中引用的范围,则它不足以满足 courses.courseWork.list API.

可以在 Google_Service_Classroom 中找到可用范围的列表,或者您可以使用 URL 本身。

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

尽量选择尽可能少的范围以完成您的需要。