简体   繁体   中英

Error when creating token with JWT

I am building an API, but I get an Uncaught error when creating my token with JWT, when I run a post man call, I get in my error log Stack trace:

#0 [internal function]: Api->generateToken()
#1 /home/osconliz/public_html/Osconlizapicall/rest.php(42): ReflectionMethod->invoke(Object(Api))
#2 /home/osconliz/public_html/Osconlizapicall/index.php(4): Rest->processApi()
#3 {main}
thrown in /home/osconliz/public_html/Osconlizapicall/api.php on line 36
[19-May-2018 02:04:47 UTC] PHP Fatal error:  Uncaught Error: Class 'JWT' not found in /home/osconliz/public_html/Osconlizapicall/api.php:36
Stack trace:
#0 [internal function]: Api->generateToken()
#1 /home/osconliz/public_html/Osconlizapicall/rest.php(42): ReflectionMethod->invoke(Object(Api))
#2 /home/osconliz/public_html/Osconlizapicall/index.php(4): Rest->processApi()
#3 {main}

but when I check my jwt file on my PHP server it has class JWT in it.

**jwt.php** page with class jwt screen shot

Then the page I am using to create the token which is **api.php**

//SECRETE_KEY is a constant for creating a pass for JWT

   <?php 
  class Api extends Rest {
    public $dbConn;  

     public function __construct(){
        parent::__construct();  
        $db = new DbConnect;
        $this->dbConn = $db->connect();

    }

    public function generateToken(){
        $client_id_key = $this->validateParameter('client_id_key', $this->param['client_id_key'], STRING);
        //$client_secret_key = $this->validateParameter('client_secret_key', $this->param['client_secret_key'], STRING);
        //client_secret_key should be commented out it is not used for validation for security purposes, only id key

        $stmt = $this->dbConn->prepare("SELECT * FROM `api_clients_properties` WHERE client_id = :client_id_key");
        $stmt->bindParam(":client_id_key", $client_id_key);
        $stmt->execute();
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!is_array($user)){
            $this->returnResponse(API_NAME_REQUIRED, "Invalid Client Id Key");
        }

        if ($user['property_status'] == "not verified"){
           $this->returnResponse(API_NAME_REQUIRED, "Property not verified, please contact admin, to verify it");   
        }

        $payload = [
           'iat' => time(),
           'iss' => 'localhost',
           'exp' => time() + (60),
           'userId' => $user['id']
        ];
        $token = JWT::encode($payload, SECRETE_KEY);
        echo $token;
    }

  }

?>

The JWT class is in the namespace Firebase\\JWT , so you will either need to use it:

use \Firebase\Jwt\Jwt;
Jwt::encode(...);

Or use its full namespace when invoking:

\Firebase\Jwt\Jwt::encode();

If you copied the file from GitHub rather than using composer to install it, you will need to comment out the namespace lines at the top of the file. so from my snapshot at the very top of the first line of jwt.php screenshot, you will comment out //namespace Firebase\\JWT; and you would not get the 500 internal server error again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM