在通过Google教室API进行学生身份验证时,如何从班级中获取已发布课程的列表?

问题描述 投票:0回答:1

如问题中所述,我正在尝试从班级中获取所有已发布课程的列表。我已经使用了课堂API文档(https://developers.google.com/classroom/quickstart/php)中的Google“ PHP快速入门”页面来开始学习。我已经成功地使经过身份验证的API客户端正常工作,并且能够打印出我的类名列表(如上述“ PHP快速入门”页面中所示)。

我尝试了两种获取作业清单的方法。

  1. 从班级的引用(学校班级和面向对象的班级!),运行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());
    

    即使我确定所讨论的课程已设置课程,它总是返回一个空数组(如下图所示。This image shows the coursework that should have been returned.

  2. 使用$ service-> courses_courseWork-> listCoursesCourseWork($ courseId)我应该能够访问课程工作列表。但是,这引发了一个错误,告诉我“请求的身份验证范围不足”。这很奇怪,因为学生应该能够访问已发布的课程,并且默认情况下listCoursesCourseWork仅返回已发布的课程。这是代码:

    # 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
    

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

php google-api google-api-php-client google-api-client google-classroom
1个回答
0
投票

您必须提供一个或多个所需的authorization scopes。如果您使用的是快速入门中引用的范围,则对于courses.courseWork.list API来说是不够的。

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

Google_Service_Classroom

尝试选择尽可能少的范围以完成所需的内容。

© www.soinside.com 2019 - 2024. All rights reserved.