Search code examples
phpgoogle-api-php-clientgoogle-classroom

google classroom announcements create + php api (materials)


I am using google Classroom google API and Google APIs Client Library for PHP.

I can add announcement, but I cannot add materials.

I would like to add files to Google Drive, but I have errors even with "link"

My code so far:

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

$client->setApplicationName("test classroom");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/classroom.courses',
    "https://www.googleapis.com/auth/classroom.rosters",
    "https://www.googleapis.com/auth/classroom.announcements",
    "https://www.googleapis.com/auth/classroom.coursework.students",
    "https://www.googleapis.com/auth/classroom.coursework.me",
]);


// $service implements the client interface, has to be set before auth call
$service = new Google_Service_Classroom($client);

$text="some text";
$link="http://someurl";

$glink = new Google_Service_Classroom_Link($link);
$glink->setUrl($link);
$params = [
    "text" => $text,
    "materials" => [
        "link" => $glink,
    ],
];
$params_obj = new Google_Service_Classroom_Announcement($params);

$service->courses_announcements->create($course_classid, $params_obj);

//tried also with:
$params = [
    "text" => $text,
    "materials" => [
        "link" => ((new Google_Service_Classroom_Material())->setLink($glink)) ,
    ],
];

the error:

"message": "materials: Link materials must have a URL.",

Solution

  • So off the top, it seems odd here that you are creating params associative arrays at all. The PHP client has methods for adding all parameters, all the way down to the leaf level. So you shouldn't see any arrays in your code. Continue to use the methods instead and it will help clean things up. If you're still having problems, I may have some time to dig on this particular item.