duanbin3021 2016-11-03 18:58
浏览 80
已采纳

带有消息'invalid json token'的未捕获异常'InvalidArgumentException'

Hello I have a problem trying to use google/apiclient

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'invalid json token' in C:\xampp\htdocs\Google\vendor\google\apiclient\src\Google\Client.php:422 Stack trace: #0 C:\xampp\htdocs\Google\app\class\google_auth.php(43): Google_Client->setAccessToken(NULL) #1 C:\xampp\htdocs\Google\app\class\google_auth.php(34): GoogleAuth->setToken(NULL) #2 C:\xampp\htdocs\Google\index.php(10): GoogleAuth->checkRedirectCode() #3 {main} thrown in C:\xampp\htdocs\Google\vendor\google\apiclient\src\Google\Client.php on line 422

My index:

<?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()): var_dump($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>

The class 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-0tiho0cbf4d1ti864ck5d1nk8rh5pcb6.apps.googleusercontent.com');//Usuario Auth Google
                $this->client->setClientSecret('
                    bMuLusxZW6ohlI3vnvPg2zRz');//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());

                return true;
            }
            return false;
        }

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

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

    }

?>

getAccessToken() is returning NULL.

  • 写回答

1条回答 默认 最新

  • dongshie8450 2016-11-03 20:05
    关注

    To me it looks like the issue might be here

    $this->client->setClientSecret('
                    bMuLusxZW6ohlI3vnvPg2zRz');//Clave Auth Google
    

    Unless that is a copy/paste error.

    $this->client->setClientSecret('bMuLusxZW6ohlI3vnvPg2zRz');//Clave Auth Google
    

    This is what I would do to fix it, first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题