如何在 php 中使用 api 从 Google Clas-s-room 中检索作业、测验分数
Posted
技术标签:
【中文标题】如何在 php 中使用 api 从 Google Clas-s-room 中检索作业、测验分数【英文标题】:How to retrieve assignment, quiz marks from Google Clas-s-room using api in php 【发布时间】:2021-08-01 14:05:08 【问题描述】:我想使用项目的 API 从 Google 课堂中读取作业或测验分数。但我不知道如何从 Google Clas-s-room 中读取分数。
请给我一些使用 php 或 Laravel 从 Google Clas-s-room 阅读作业或测验分数的建议和源代码。
我已经在quickstart.php
文件中添加了一些代码:
<?php
require __DIR__ . '/vendor/autoload.php';
// if (php_sapi_name() != 'cli')
// throw new Exception('This application must be run on the command line.');
//
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
$client = new Google_Client();
$client->setApplicationName('Google Clas-s-room API & PHP');
$client->setScopes(array(
Google_Service_Clas-s-room::CLAs-s-rOOM_COURSES,
Google_Service_Clas-s-room::CLAs-s-rOOM_STUDENT_SUBMISSIONS_STUDENTS_READONLY,
Google_Service_Clas-s-room::CLAs-s-rOOM_ROSTERS)
);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath))
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired())
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken())
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
else
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken))
throw new Exception(join(', ', $accessToken));
// Save the token to a file.
if (!file_exists(dirname($tokenPath)))
mkdir(dirname($tokenPath), 0700, true);
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
return $client;
// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Clas-s-room($client);
// set these parameters:
// 328776504166 <- It is my course id
// 339429593407 <- It is my course work id
$courseId = "328776504166";
$courseWorkId = "339429593407";
$results = $service->courses_courseWork_studentSubmissions->listCoursesCourseWorkStudentSubmissions($courseId, $courseWorkId);
foreach ($results->studentSubmissions as $r => $submission)
$student = $service->courses_students->get($courseId, $submission->userId);
$studentName = $student->profile->name->fullName;
print($studentName . ": ");
print($submission->assignedGrade. "\n");
然后当我在本地主机上运行quickstart.php
时,可以看到以下问题:
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"
我找不到我的错误。如何解决这个问题呢?请给我一些建议
【问题讨论】:
请参阅How to Ask、help center。 请将您的文本示例(代码、错误等)粘贴为文本,并使用格式化工具(如下面的答案)。你会编辑你的问题吗? 【参考方案1】:答案:
您可以使用courses.courseWork.studentSubmissions.list
方法检索学生提交的课程作业列表。在响应中,会有assignedGrade
字段。
示例:
// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Clas-s-room($client);
// set these parameters:
$courseId = "180119025944";
$courseWorkId = "177950380066";
$results = $service->courses_courseWork_studentSubmissions->listCoursesCourseWorkStudentSubmissions($courseId, $courseWorkId);
foreach ($results->studentSubmissions as $r => $submission)
$student = $service->courses_students->get($courseId, $submission->userId);
$studentName = $student->profile->name->fullName;
print($studentName . ": ");
print($submission->assignedGrade. "\n");
另外,请确保您设置了正确的范围:
$client->setScopes(array(
Google_Service_Clas-s-room::CLAs-s-rOOM_COURSES,
Google_Service_Clas-s-room::CLAs-s-rOOM_STUDENT_SUBMISSIONS_STUDENTS_READONLY,
Google_Service_Clas-s-room::CLAs-s-rOOM_ROSTERS)
);
参考资料:
PHP Quickstart | Clas-s-room API | Google Developers Google Clas-s-room API - PHP Reference Method: courses.courseWork.studentSubmissions.list | Clas-s-room API | Google Developers【讨论】:
你好拉法吉列尔莫。首先感谢您的宝贵建议和指导。我已经在 quickstart.php 文件中添加了一些代码。但是当我在本地主机上运行 quickstart.php 时,可以看到一些问题。要查看问题请打开此链接i.stack.imgur.com/4cQig.pngi.stack.imgur.com/YlOHd.png 请告诉我我哪里出错了?谢谢 我认为仅仅因为答案被否决而删除答案不符合 Stack Overflow 的精神。在 10K 代表,你可以经受住奇数 -2!:=)
@Md.Mahmud 我已经更新了我的答案; courseId 和 submit 不应该有 obj 引用,所以我删除了它们。
@RafaGuillermo 你好。我已根据您的建议更新了我的 quickstart.php 文件。我必须删除课程 ID 和提交。但是当我在本地主机上运行 quickstart.php 时,可以看到一些问题。要查看代码请打开此链接:i.stack.imgur.com/LcVjV.png 要查看问题请打开此链接:i.stack.imgur.com/wfQFh.png 请给我一些建议并告诉我哪里出错了?谢谢。
错误很明显,你没有正确的范围来做你想做的事。你有什么范围?根据此 sn-p,您将至少需要 https://www.googleapis.com/auth/clas-s-room.coursework.students.readonly
和 https://www.googleapis.com/auth/clas-s-room.rosters.readonly
,分别为 courses.courseWork.studentSubmissions.get 和 courses.students.get 的文档页面以上是关于如何在 php 中使用 api 从 Google Clas-s-room 中检索作业、测验分数的主要内容,如果未能解决你的问题,请参考以下文章
如何在 php 中使用 Google Fit api 获取步数?
Google BigQuery - PHP API - 如何导入 AVRO 文件?