未捕获的异常' Firebase \ JWT \ BeforeValidException'消息'无法在2016-11-03T21:37:13 + 0100'之前处理令牌

时间:2016-11-03 20:51:31

标签: php google-api-php-client

您好我尝试使用google / apiclient

时遇到问题
  

致命错误:未捕获的异常' Firebase \ JWT \ BeforeValidException'   消息'无法在2016-11-03T21:37:13 + 0100'之前处理令牌   在C:\ xampp \ htdocs \ Google \ vendor \ firebase \ php-jwt \ src \ JWT.php:124   堆栈跟踪:#0   C:\ XAMPP \ htdocs中\谷歌\厂商\谷歌\ apiclient \ SRC \谷歌\的accessToken \ Verify.php(100):   Firebase \ JWT \ JWT :: decode(' eyJhbGciOiJSUzI ...',' ----- BEGIN PUBL ......',   数组)#1   C:\ XAMPP \ htdocs中\谷歌\厂商\谷歌\ apiclient \ SRC \谷歌\ Client.php(705):   Google_AccessToken_Verify-> verifyIdToken(' eyJhbGciOiJSUzI ...&#39 ;,   ' 474251646530-0t ...')#2   C:\ XAMPP \ htdocs中\谷歌\程序\类\ google_auth.php(51):   Google_Client-> verifyIdToken()#3   C:\ XAMPP \ htdocs中\谷歌\程序\类\ google_auth.php(35):   GoogleAuth-> getPayLoad()#4 C:\ xampp \ htdocs \ Google \ index.php(10):   GoogleAuth-> checkRedirectCode()#5 {main}引入   第124行的C:\ xampp \ htdocs \ Google \ vendor \ firebase \ php-jwt \ src \ JWT.php

我的索引:

<?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;
        }

    }

?>

请帮助我

3 个答案:

答案 0 :(得分:3)

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

当库和服务器之间的时差大于余地

时,会发生此错误

要解决此问题,请转到

  

\厂商\谷歌\ apiclient \ SRC \谷歌\的accessToken \ Verify.php

并增加 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;
  }

答案 1 :(得分:1)

您遇到服务器时间问题。 JWT Library和Server之间的时间不同。该库使用“UTC”时区。根据您的用例,您需要将时区对齐到同一区域,或者如果您不需要,可以对此行进行注释。

// Configures the time that the token can be used (nbf claim)
// ->setNotBefore(time() + 60)

答案 2 :(得分:0)

在此功能内:

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

删除getAttributes()并将其保留为:

public function getPayLoad(){
    $payload=$this->client->verifyIdToken();
    //print_r($payload); //This gives you the information you need
    return $payload;
}