2

I am writing an app in PHP which will connect to my domains's Google Classroom. However I get the following error when I try to do anything with the Google Classroom API:

Message: Error calling GET https://www.googleapis.com/v1/courses?pageSize=100: (404) Not Found

My code so far:

$scopes = 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'
);

$gServiceEmail = "[email protected]";
$gServiceKey = file_get_contents("../path/to/cert.p12");

$client = new Google_Client();
$gAuth = new Google_Auth_AssertionCredentials(
  $gServiceEmail,
  $scopes,
  $gServiceKey
);

$gAuth->sub = "[email protected]";
$client->setAssertionCredentials($gAuth);

$service = new Google_Service_Classroom($client);
$results = $service->courses->listCourses();

I have enabled the scopes in the API Settings in the Google Admin Console for the service account and enabled the api in the developer console. Where am I going wrong?

2 Answers 2

5

I think your endpoint is wrong according to the documentation of Classroom API. Try changing it to https://classroom.googleapis.com

sample request:

GET https://classroom.googleapis.com/v1/courses?pageSize=100&key={YOUR_API_KEY}
1
  • I had an old version of the php client lib. Upgrading to the latest version resolved the issue as it uses the correct endpoint as you state in your answer
    – amburnside
    Dec 23, 2015 at 16:09
0

Course with id is not found. For a list of courses, use the courses.list(), as shown in the following sample.

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

// Print the first 10 courses the user has access to.
$optParams = array(
  'pageSize' => 10
);
$results = $service->courses->listCourses($optParams);

if (count($results->getCourses()) == 0) {
  print "No courses found.\n";
} else {
  print "Courses:\n";
  foreach ($results->getCourses() as $course) {
    printf("%s (%s)\n", $course->getName(), $course->getId());
  }
}

References:

https://developers.google.com/classroom/quickstart/php?hl=en https://developers.google.com/classroom/guides/manage-courses?hl=en

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.