PERMISSION_DENIED 课堂 api 上交时即使课程作业也是使用课堂 api 创建的

PERMISSION_DENIED classroom api when turn in even when the course work was created using classroom api too

您好,我正在开发一个系统,我们将 google 课堂整合到其中。所以我在提交学生提交时使用教室 API。我收到一条错误消息,提示权限被拒绝。我也从其他文章中读到,当 coursework/assignment 本身不是使用 Google 脚本或使用课堂 API 创建时,它得到了拒绝的许可。因此,我使用教室 api 在该系统中创建了另一个 coursework/assignment。我尝试了 modifyAttachment 函数,它运行良好,但是当我使用 turnIn 函数时,权限被拒绝。我正在使用 laravel 框架。

我的代码:

        $appName = 'PROGRAMA';
        $client = new Google_Client();
        $client->setApplicationName($appName);
        $client->setScopes([
            Google_Service_Classroom::CLASSROOM_COURSES,
            Google_Service_Classroom::CLASSROOM_COURSES_READONLY,
            Google_Service_Classroom::CLASSROOM_COURSEWORK_ME,
            Google_Service_Classroom::CLASSROOM_COURSEWORK_STUDENTS,
            Google_Service_Classroom::CLASSROOM_COURSEWORK_STUDENTS_READONLY,
            Google_Service_Classroom::CLASSROOM_ROSTERS,
            Google_Service_Classroom::CLASSROOM_STUDENT_SUBMISSIONS_ME_READONLY,
            // Google_Service_Classroom::CLASSROOM_ANNOUNCEMENTS_READONLY,
            Google_Service_Classroom::CLASSROOM_COURSEWORKMATERIALS_READONLY,
            "https://www.googleapis.com/auth/drive",
            "https://www.googleapis.com/auth/drive.file",
            "https://www.googleapis.com/auth/classroom.coursework.me"                             
          ]);
        $client->setAuthConfig(storage_path().'/programa-classroom-43bf78f68328.json');
        
        // // this is needed only if you need to perform
        // // domain-wide admin actions, and this must be
        // // an admin account on the domain; it is not 
        // // necessary in your example but provided for others
        $client->setSubject('programa_dev@gbox.adnu.edu.ph');

        $service  = new Google_Service_Classroom($client);
        
        $driveservice = new Google_Service_Drive($client);

        $driveID = $service->courses_courseWork_studentSubmissions->listCoursesCourseWorkStudentSubmissions(session('courseInfo')['courseID'], 
        session('courseInfo')['courseWorkID'], $optParams = array("userId" => session('courseInfo')['userID']));

        $file = new Google_Service_Drive_DriveFile();
        $file->setName(session('courseInfo')['StudentName'] . '-' . session('courseInfo')['AssignmentTitle']);
        $assignWork = $request->file('courseAssignment');
        $filename = session('courseInfo')['StudentName'] . '-' .$assignWork->getClientOriginalName();
        $path = storage_path(). 'CourseWork/' . session('courseInfo')['AssignmentTitle'];
        $fileAssignment = $request->file('courseAssignment')->storeAs('public',$filename);
        $contents = Storage::disk('public')->path($filename);
        foreach($driveID as $row)
        {
            if($row->state == "CREATED")
            {
                $FileID = $row->assignmentSubmission['attachments'][0]['driveFile']["id"];
            }
            else
            {
                $result = $driveservice->files->create(
                    $file,
                    [
                    'data' => file_get_contents($contents),
                    'mimeType' => 'application/octet-stream',
                    'uploadType' => 'multipart '
                    ]
                );

                $FileID = $result->id;

                $modifyParams = array(
                    "addAttachments" => array(
                        "driveFile" => array(
                            "id" => $FileID
                        )
                    )
                );

            $data['modify'] = $service->courses_courseWork_studentSubmissions->modifyAttachments(session('courseInfo')['courseID'], 
            session('courseInfo')['courseWorkID'], session('courseInfo')['submissionID'], new Google_Service_Classroom_ModifyAttachmentsRequest($modifyParams));
            }
        }
        
    $data['turnIn'] = $service->courses_courseWork_studentSubmissions->turnIn(session('courseInfo')['courseID'], session('courseInfo')['courseWorkID'], 
        session('courseInfo')['submissionID'], new Google_Service_Classroom_TurnInStudentSubmissionRequest());

error message here

让我们看看我是否可以在不看您的代码的情况下解释您的问题。

您需要了解的是私有数据和 public 数据之间的区别。 Public 数据是不为任何人所有的数据私人数据为某人所有,您需要该人的许可才能访问它。

如果您勾选 Classroom api 每个方法都在此处表示。

访问私人用户数据的每种方法都会告诉您需要什么访问权限才能访问它

当您编写应用程序时,您添加了一个称为范围的东西,这些定义了您的应用程序需要的访问范围,以便 运行。您收到的错误消息意味着您正在尝试访问尚未被授权使用的方法。所以你需要修复这些问题并再次请求用户授权。

我们解决了这个问题。我的错误是我在执行 turnIn 函数时使用了服务帐户,它应该是拥有提交的学生。所以我们为学生重新验证它并在执行 turnIn 函数时使用该帐户。

非常感谢那些提供帮助的人。

正如方法文档所指定的那样,courses.courseWork.studentSubmissions.turnIn 只能被调用:

the student that owns the specified student submission.

如果使用 domain-wide delegation 的服务帐户,您应该冒充该学生。否则,唯一的选择是以拥有提交的学生身份执行此操作。