【问题标题】:Why do I get 404 Error when using Google Classroom API?为什么我在使用 Google Classroom API 时会收到 404 错误?
【发布时间】:2016-03-28 18:34:07
【问题描述】:

我正在用 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。我哪里错了?

【问题讨论】:

    标签: php google-api-php-client google-classroom


    【解决方案1】:

    找不到带有 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

    【讨论】:

      【解决方案2】:

      根据 Classroom API 的documentation,我认为您的端点是错误的。试试改成https://classroom.googleapis.com

      样品请求:

      GET https://classroom.googleapis.com/v1/courses?pageSize=100&key={YOUR_API_KEY}
      

      【讨论】:

      • 我有一个旧版本的 php 客户端库。升级到最新版本解决了该问题,因为它使用了您在回答中所述的正确端点
      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2018-01-03
      相关资源
      最近更新 更多