Php 无法邀请用户访问Google教室

Php 无法邀请用户访问Google教室,php,api,curl,google-classroom,Php,Api,Curl,Google Classroom,我试图用以下PHP代码邀请用户访问Google教室: $paramertos = array( "role" => "STUDENT", "userId" => $mail, "courseId" => $courseId ); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://classroom.googleapis.com/v

我试图用以下PHP代码邀请用户访问Google教室:

$paramertos = array(
    "role" => "STUDENT", 
    "userId" => $mail, 
    "courseId" => $courseId
);

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://classroom.googleapis.com/v1/invitations",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $paramertos,
    CURLOPT_HTTPHEADER => array(
        "authorization: Bearer $access_token"
    )
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
    echo "cURL Error:" . $err;
} else {
    $response = json_decode($response, true);
    var_dump($response);
}
结果是:

array(1) { 
    ["error"]=> array(3) {
        ["code"]=> int(400) 
        ["message"]=> string(37) "Request contains an invalid argument."
        ["status"]=> string(16) "INVALID_ARGUMENT" 
    }
 } 
$access\u令牌、$mail和$courseId是正确的


任何帮助都将不胜感激。

错误400表示您创建了一个错误请求

如果查询参数格式不正确,则\u参数无效

为了便于编码,您可以使用。下面是一个示例代码,可以帮助您了解更多关于GooglePHP客户端库的信息。不幸的是,我没有任何用于创建邀请的代码示例

$courseId = '123456';
$teacherEmail = 'alice@example.edu';
$teacher = new Google_Service_Classroom_Teacher(array(
  'userId' => $teacherEmail
));
try {
  $teacher = $service->courses_teachers->create($courseId, $teacher);
  printf("User '%s' was added as a teacher to the course with ID '%s'.\n",
      $teacher->profile->name->fullName, $courseId);
} catch (Google_Service_Exception $e) {
  if ($e->getCode() == 409) {
    printf("User '%s' is already a member of this course.\n", $teacherEmail);
  } else {
    throw $e;
  }
}

希望这有帮助。

我在代码中找到了一个解决方案。找不到邀请的文档

$service = new Google_Service_Classroom($client);

$googleInvitation = new Google_Service_Classroom_Invitation(
    array("role" => "STUDENT",
    "userId" => $mail,
    "courseId" => $courseId)
);
try{
    $invitationRes = $service->invitations->create($googleInvitation);
}catch(Exception $exception){
    echo $exception;
    die();
}
var_dump($invitationRes);

你为什么不通过网站来做呢?你是想用它来实现功能,还是仅仅作为一个有趣的编码挑战?我需要它来集成创建课程和注册用户两者都可以,但我需要邀请用户问题出在哪里。谢谢,但我使用Google API客户端库来获取、列出、更新、创建课程和注册课程用户。对于“删除课程”,获取用户配置文件并创建“我无法使用库”邀请。删除课程并让用户配置文件正常工作