【问题标题】:Using Google Http Batch with Google Classroom Api将 Google Http Batch 与 Google Classroom Api 一起使用
【发布时间】:2017-01-11 15:14:49
【问题描述】:

我正在编写一个应用程序(使用 PHP),它将学生添加到 Google 课堂。我正在关注以下文档:

https://developers.google.com/classroom/guides/batch

我正在使用批量请求将多个学生添加到 Google 课堂。但是,批处理请求似乎失败了。我的代码如下:

$service = new Google_Service_Classroom($client);

$service->getClient()->setUseBatch(true);

$batch = $service->createBatch();

$courseId = "123456";
$studentEmails = ["user1@domain.com","user2@domain.com"];

foreach($studentEmails as $email) {
  $student = new Google_Service_Classroom_Student(['userId' => $email]);
  $request = $service->courses_students->create($courseId, $student);
  $requestId = $email;
  $batch->add($request, $requestId);
}

$results = $batch->execute();


foreach($results as $responseId => $student) {
  $studentEmail = substr($responseId, strlen('response-') );
  if ($student instanceof Google_Service_Exception) {
    $e = $student;
    printf("Error adding user '%s' to the course: %s\n", $studentEmail,
      $e->getMessage());
  } else {
    printf("User '%s' was added as a student to the course.\n",
        $student->profile->name->fullName, $courseId);
  }
}

$service->getClient()->setUseBatch(false);

这段代码的输出是:

Error adding user 'user1@domain.com' to the course: Not Found ...

但是,用户和课程都存在于域中。如果我删除批处理代码并一次发出一个请求,学生会成功添加到课堂,这让我相信我在批处理请求中遗漏了一些东西

【问题讨论】:

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


    【解决方案1】:

    您可能需要先检查PHP Quickstart,并确保您完成了给定页面其余部分中描述的步骤,以便能够向 Classroom API 发出请求,如批处理请求中的using client libraries 中所述。

    成功安装并设置库后,您可以尝试代码示例,该示例演示如何使用 Google API 客户端库发出批处理请求。

    $courseId = '123456';
    $studentEmails = array('alice@example.edu', 'bob@example.edu');
    $service->getClient()->setUseBatch(true);
    $batch = $service->createBatch();
    foreach($studentEmails as $studentEmail) {
      $student = new Google_Service_Classroom_Student(array(
        'userId' => $studentEmail
      ));
      $request = $service->courses_students->create($courseId, $student);
      $requestId = $studentEmail;
      $batch->add($request, $requestId);
    }
    $results = $batch->execute();
    foreach($results as $responseId => $student) {
      $studentEmail = substr($responseId, strlen('response-') + 1);
      if ($student instanceof Google_Service_Exception) {
        $e = $student;
        printf("Error adding user '%s' to the course: %s\n", $studentEmail,
            $e->getMessage());
      } else {
        printf("User '%s' was added as a student to the course.\n",
            $student->profile->name->fullName, $courseId);
      }
    }
    $service->getClient()->setUseBatch(false);
    

    【讨论】:

    • 感谢您的帖子,我已通读快速入门文档并已成功在其他谷歌服务上使用批处理请求。将学生招入教室时似乎并没有打球
    猜你喜欢
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2021-05-09
    • 2014-03-04
    • 2012-09-12
    • 2014-11-25
    • 2016-11-06
    • 2023-03-07
    相关资源
    最近更新 更多