Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

Google Classroom - Create Course Error PHP

I'm trying to create a course using the Google_Service_Classroom API, using PHP, but I get an error in the call.

<?php
require_once __DIR__.'/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=C:laragonwwwclassroom.json');

$user_admin= '[email protected]';

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject($user_admin);

$client->setScopes(array(

    'https://www.googleapis.com/auth/classroom.courses',
    'https://www.googleapis.com/auth/classroom.courses.readonly',
    'https://www.googleapis.com/auth/classroom.rosters',
    'https://www.googleapis.com/auth/classroom.rosters.readonly',
    'https://www.googleapis.com/auth/classroom.profile.emails',
    'https://www.googleapis.com/auth/classroom.profile.photos'

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

$postBody = array(
    "id" => "1",
    "name" => "Course 01",
    "section" => "15/19",
    "descriptionHeading" => "Course 01 test 001",
    "description" => "Course 01 test",
    "room" => "03/12",
    "creationTime" => "2020-12-12T11:48:50.951Z",
    "enrollmentCode" => "yzdeeee",
    "courseState" => "ACTIVE",
    "alternateLink" => "https://classroom.google.com/c/coursetest01"
);

$optParams = array();
$results = $service->courses->create($postBody, $optParams);

echo '<pre>', print_r($results, true);
exit;

My return:

Fatal error: Uncaught TypeError: Argument 1 passed to Google_Service_Classroom_Resource_Courses::create() must be an instance of Google_Service_Classroom_Course, array given, called in C:laragonwwweste-03.php on line 41 and defined in C:laragonwwwvendorgoogleapiclient-servicessrcGoogleServiceClassroomResourceCourses.php:47 Stack trace: #0 C:laragonwwweste-03.php(41): Google_Service_Classroom_Resource_Courses->create(Array, Array) #1 {main} thrown in C:laragonwwwvendorgoogleapiclient-servicessrcGoogleServiceClassroomResourceCourses.php on line 47

I'm seeing the documents, but I don't see where to fix this problem.

question from:https://stackoverflow.com/questions/65601530/google-classroom-create-course-error-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error message is self-explanatory. It says Argument 1 passed to Google_Service_Classroom_Resource_Courses::create() must be an instance of Google_Service_Classroom_Course, array given and this occurs because $postBody is an array.

As shown in the documentation of function create

create( Google_Service_Classroom_Course $postBody, array $optParams = array() )

this function expects the first parameter to be a Google_Service_Classroom_Course object instead of an array. So you need to first create an object of this type and then pass this object as parameter. So the end result should be like:

$course = new Google_Service_Classroom_Course(array(
    "id" => "1",
    "name" => "Course 01",
    "section" => "15/19",
    "descriptionHeading" => "Course 01 test 001",
    "description" => "Course 01 test",
    "room" => "03/12",
    "creationTime" => "2020-12-12T11:48:50.951Z",
    "enrollmentCode" => "yzdeeee",
    "courseState" => "ACTIVE",
    "alternateLink" => "https://classroom.google.com/c/coursetest01"
));


$course = $service->courses->create($course);

note: since the second parameter has a default value of empty array (array $optParams = array()) it is not necessary to include it explicitly. The last line of the above is exactly the same with:

$optParams = array();
$course = $service->courses->create($course, $optParams);

Also have in mind that since $course is an object its not advised to print it like you do with arrays. You can instead print specific info of the object like:

printf("Course created: %s (%s)
", $course->name, $course->id);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...