image
后端代码php有引用两个composer包
composer require google/apiclient firebase/php-jwt
public function google() { $param = $this->request->param(); trace($param, 39;error'); $jwksUrl = 'https://www.googleapis.com/oauth2/v3/certs'; // 获取谷歌的公钥 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $jwksUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); if ($response) { $jwks = json_decode($response, true); // 解码和验证 JWT try { $decoded = JwT::decode($param['credential'], JWK::parseKeySet($jwks), ['RS256']); $json = json_encode($decoded); // 获取用户信息 $email = $decoded->email; $name = $decoded->name; $picture = $decoded->picture; // 在这里处理用户信息,例如登录或注册用户 } catch (Exception $e) { echo '验证令牌失落败: ', $e->getMessage(), "\n"; } } else { echo '无法获取谷歌公钥'; } }
以上代码仅供参考。如遇版本更新,可能有所不同。