您好,我在尝试使用 google/apiclient 时遇到问题



我的指数:

<?php
    require_once('app/ini.php');
    require_once('vendor/autoload.php');
    require_once('app/class/google_auth.php');


    $googleClient = new Google_Client();
    $auth = new GoogleAuth($googleClient);

    if ($auth->checkRedirectCode()) {
        header("Location: index.php");
    }

?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <?php if (!$auth->isLoggedIn()): //Verificar Inicio de Sesion ?>
        <a href="<?php echo $auth->getAuthUrl(); ?>">Inicie Sesion con Google</a>
    <?php else: //Si no ha iniciado Sesion ?>
        Bienvenido.. <a href="logout.php">Cerrar Sesion</a>
    <?php endif; ?>

</body>
</html>

类 GoogleAuth:
<?php

    class GoogleAuth{//Clase para la autenticacion del usuario google

        protected $client;//Variable de cliente

        public function __construct(Google_Client $googleClient = null){
            $this->client = $googleClient;

            if ($this->client) {

                $this->client->setClientId('474251646530-0tiho0cbf4dusercontent.com');//Usuario Auth Google
                $this->client->setClientSecret('bMuLusxvnvPg2zRz');//Clave Auth Google
                $this->client->setRedirectUri('http://localhost/Google/index.php');
                $this->client->setScopes('email');

            }
        }

        public function isLoggedIn(){//Metodo que devuelve el estatus de la Sesion con Google (true o false)
            return isset($_SESSION['access_token']);

        }

        public function getAuthUrl(){//Funcion que devuelve el enlace requerido para iniciar sesion
            return $this->client->createAuthUrl();

        }

        public function checkRedirectCode(){
            if (isset($_GET['code'])) {
                $this->client->authenticate($_GET['code']);
                $this->setToken($this->client->getAccessToken());

                $payload=$this->getPayLoad();
                echo "<pre>", print_r($payload) ,"<pre>";
                return true;
            }
            return false;
        }

        public function setToken($token){
            $_SESSION['access_token']=$token;
            $this->client->setAccessToken($token);
        }

        public function logout(){
            unset($_SESSION['access_token']);
        }

        public function getPayLoad(){
            $payload=$this->client->verifyIdToken()->getAttributes();
            return $payload;
        }

    }

?>

请帮我

最佳答案

JWT 库利用余地(以秒为单位)来说明签名和验证服务器之间何时存在时钟偏差时间。

当库和您的服务器之间的时间差异大于回旋余地时会发生此错误

要修复它,请转到



并在 getJwtService 函数中增加回旋余地。

private function getJwtService()
  {
    $jwtClass = 'JWT';
    if (class_exists('\Firebase\JWT\JWT')) {
      $jwtClass = 'Firebase\JWT\JWT';
    }

    if (property_exists($jwtClass, 'leeway')) {
      // adds 1 second to JWT leeway
      // @see https://github.com/google/google-api-php-client/issues/827
      $jwtClass::$leeway += 10;
    }

    return new $jwtClass;
  }

关于php - 带有消息 'Firebase\JWT\BeforeValidException' 的未捕获异常 'Cannot handle token prior to 2016-11-03T21:37:13+0100',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40411014/

10-11 16:16