我正在为我的谷歌登录制作后端身份验证系统,这将验证令牌,提取相关数据并创建新用户或检查当前的用户,如下所示:
// Get $id_token via HTTPS POST.
if(isset($_GET['id_token']) && !(empty($_GET['id_token']))) {
$id_token = $_GET['id_token'];
$CLIENT_ID ='client_id.apps.googleusercontent.com';
echo "1\n";
$client = new Google_Client(['client_id' => $CLIENT_ID]);
echo "2\n";
$payload = $client->verifyIdToken($id_token);
//$payload = $client->verifyIdToken($id_token);
echo "3\n";
if ($payload) {
$result = array(
"id" => $payload['sub'],
"email" => $payload['email'],
"name" => $payload['name'],
"source" => $payload['iss'],
"picture" => $payload['picture'],
);
$query = mysqli_query($conn, "SELECT * FROM oauth_users WHERE id='".$payload['sub']."'");
if (!$query) { die('Error: ' . mysqli_error($conn)); }
if(!(mysqli_num_rows($query) > 0)){
$sql = "INSERT INTO oauth_users (id, email, name, source, picture) VALUES ('" . $payload['sub'] . "', '" . $payload['email'] . "','" . $payload['name'] . "', '" . $payload['iss'] . "', '" . $payload['picture'] . "')";
if ($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
echo "[" . json_encode($result) . "]";
} else {
echo "Error: failed to validate";
}
}
此代码在本地工作,我将其托管在我的服务器上并尝试运行它,但每次到达此行时都会收到500内部服务器错误:
$payload = $client->verifyIdToken($id_token);
这段代码,至少直接从googles oauth PHP文档中复制了准系统版本,所以我不确定是什么玩法。 https://developers.google.com/identity/sign-in/web/backend-auth
(它将回应&#39; 1..2..500内部错误&#39;)