Php JWT令牌返回为空,即使该令牌是在postman中创建的
我正在用JWT构建一个小API,在客户端使用其客户端id密钥发送令牌后,我在API中使用客户端id密钥创建令牌,然后我尝试将该令牌取回,在成功创建令牌后的postman中,但我得到了响应未找到访问令牌,这是我设置为响应的,如果它在我创建的常量中找不到令牌,这是在constant.php中,请询问我做错了什么,我已尝试在源代码上编辑代码,但什么都没有。请如果我的文章需要更正,请编辑将不胜感激Php JWT令牌返回为空,即使该令牌是在postman中创建的,php,json,api,jwt,token,Php,Json,Api,Jwt,Token,我正在用JWT构建一个小API,在客户端使用其客户端id密钥发送令牌后,我在API中使用客户端id密钥创建令牌,然后我尝试将该令牌取回,在成功创建令牌后的postman中,但我得到了响应未找到访问令牌,这是我设置为响应的,如果它在我创建的常量中找不到令牌,这是在constant.php中,请询问我做错了什么,我已尝试在源代码上编辑代码,但什么都没有。请如果我的文章需要更正,请编辑将不胜感激 api.php //where I generate my token and create_inser
api.php //where I generate my token and create_insert_new_delivery() is the function that checks the incoming parameter for adding a new delivery according to the API i am building
public function generateToken(){ //WORKS PERFECTLY, BUT WHEN I TRY TO GET THE TOKEN
try {
$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);
$data = ['token' => $token];
$this->returnResponse(SUCCESS_RESPONSE, $data);
} catch (Exception $e){
$this->throwError(JWT_PROCESSING_ERROR, $e->getMessage());
}
}
public function create_insert_new_delivery(){
$c_payment_type = $this->validateParameter('payment_type', $this->param['payment_type'], STRING, true);
$c_date_of_delivery = $this->validateParameter('date_of_delivery', $this->param['date_of_delivery'], STRING, true);
$c_country_from = $this->validateParameter('country_from', $this->param['country_from'], STRING, true);
}
try {
echo $token = $this->getBearerToken(); //Trying to get this token out, but its empty what am i doing wrong.
} catch (Exception $e){
$this->throwError(ACCESS_TOKEN_ERRORS, $e->getMessage()); //THIS "ACCESS_TOKEN_ERRORS" gives undefined token
}
}
//constants.php the constant to serve errors
/*Server Errors*/
define('JWT_PROCESSING_ERROR', 300);
define('AUTHORIZATION_HEADER_NOT_FOUND', 301);
define('ACCESS_TOKEN_ERRORS', 302);
对于将来可能出现问题的人,请确保您的授权值之间的空格(即承载者)是准确的,它们之间只有一个空格,如承载者您的.TOKEN。在这里您可以像我在问题中所做的那样回显您的令牌,您将获得所需的令牌