2

我正在用 PHP 编写一个应用程序,它将连接到我的域的 Google 课堂。但是,当我尝试使用 Google Classroom API 执行任何操作时,出现以下错误:

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

到目前为止我的代码:

$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 = "random@developer.gserviceaccount.com";
$gServiceKey = file_get_contents("../path/to/cert.p12");

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

$gAuth->sub = "user@mydomain.com";
$client->setAssertionCredentials($gAuth);

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

我已经在 Google 管理控制台的 API 设置中为服务帐户启用了范围,并在开发者控制台中启用了 api。我哪里错了?

4

2 回答 2

5

根据Classroom API的文档,我认为您的端点是错误的。尝试将其更改为 https://classroom.googleapis.com

样品要求:

GET https://classroom.googleapis.com/v1/courses?pageSize=100&key={YOUR_API_KEY}
于 2015-12-23T16:07:14.933 回答
0

找不到具有 ID 的课程。对于课程列表,请使用 courses.list(),如以下示例所示。

$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());
  }
}

参考:

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

于 2021-08-30T17:05:37.550 回答