Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php中的api从Google教室检索作业、测验分数_Php_Google Api_Google Api Php Client_Google Api Client_Google Classroom - Fatal编程技术网

如何使用php中的api从Google教室检索作业、测验分数

如何使用php中的api从Google教室检索作业、测验分数,php,google-api,google-api-php-client,google-api-client,google-classroom,Php,Google Api,Google Api Php Client,Google Api Client,Google Classroom,我想阅读作业或测验分数从谷歌教室使用API的项目。但我不知道如何从谷歌教室里读分数 请给我一些使用PHP或Laravel阅读谷歌课堂作业或测验分数的建议和源代码 我已经在quickstart.php文件中添加了一些代码: <?php require __DIR__ . '/vendor/autoload.php'; // if (php_sapi_name() != 'cli') { // throw new Exception('This application must be

我想阅读作业或测验分数从谷歌教室使用API的项目。但我不知道如何从谷歌教室里读分数

请给我一些使用PHP或Laravel阅读谷歌课堂作业或测验分数的建议和源代码

我已经在
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 Classroom API & PHP');

    $client->setScopes(array(
        Google_Service_Classroom::CLASSROOM_COURSES, 
        Google_Service_Classroom::CLASSROOM_STUDENT_SUBMISSIONS_STUDENTS_READONLY, 
        Google_Service_Classroom::CLASSROOM_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_Classroom($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");
}
}

我找不到我的错。如何解决这个问题?请给我一些建议

回答: 您可以使用该方法检索学生提交的课程作业列表。在响应中,将出现
assignedGrade
字段

例子: 此外,请确保设置了正确的作用域:

$client->setScopes(array(
  Google_Service_Classroom::CLASSROOM_COURSES, 
  Google_Service_Classroom::CLASSROOM_STUDENT_SUBMISSIONS_STUDENTS_READONLY, 
  Google_Service_Classroom::CLASSROOM_ROSTERS)
);
我希望这对你有帮助

参考资料:

请参见,。请将文本示例(代码、错误等)粘贴为文本,并使用格式化工具(如下面的答案)。你能编辑你的问题吗?你好,拉法·吉勒莫。首先,感谢您提出的宝贵建议和指导方针。我已经在quickstart.php文件中添加了一些代码。但是,当我在localhost上运行quickstart.php时,可以看到一些问题。要查看问题,请打开此链接。请告诉我哪里出了问题?谢谢,我认为仅仅因为得到了否决票就删除一个答案并不符合堆栈溢出的精神。在10公里重复赛中,你可以通过奇数-2<代码>:=)@Md.Mahmud我已经更新了我的答案;courseId和submission不应该有obj引用,所以我已经删除了它们。@RafaGuillermo您好。我已根据您的建议更新了quickstart.php文件。我必须删除课程Id和提交。但是,当我在localhost上运行quickstart.php时,可以看到一些问题。要查看代码,请打开此链接:要查看问题,请打开此链接:请给我一些建议,并告诉我哪里出错?谢谢。错误很明显,您没有正确的范围来执行您正在尝试的操作。你们有什么望远镜?您将需要
https://www.googleapis.com/auth/classroom.coursework.students.readonly
https://www.googleapis.com/auth/classroom.rosters.readonly
至少基于此片段,分别按照和的文档页面
// 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_Classroom($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_Classroom::CLASSROOM_COURSES, 
  Google_Service_Classroom::CLASSROOM_STUDENT_SUBMISSIONS_STUDENTS_READONLY, 
  Google_Service_Classroom::CLASSROOM_ROSTERS)
);