php - 即使令牌是在邮递员中创建的,JWT 令牌仍为空
问题描述
我正在使用 JWT 构建一个小型 API,并且在客户端使用其客户端 ID 密钥将其发送到 API 中创建带有客户端 ID 密钥的令牌之后,我尝试在创建令牌后在邮递员中取回该令牌成功但我得到响应 访问令牌未找到,这是我设置为响应,如果它无法在我创建的常量中找到令牌,该令牌位于 constant.php 中,请问我做错了什么,我试图编辑代码在我的源代码上,但什么都没有。请如果我的帖子需要更正,请进行编辑。
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);
解决方案
推荐阅读
- javascript - 如何为我的图像幻灯片修复此代码?
- c++ - glfw 需要链接什么?
- rust - 如何在 Rust 中测试类型相等性?
- mysql - 完全访问用户的数据库权限错误
- python-3.x - AttributeError:硒上的“NoneType”
- java - 为什么 Quartz Scheduler 在 x 分钟后停止在 Heroku 上工作?
- jquery - Zendesk Ticket 使用 ajax 创建 API
- php - PDFLib 已安装但在代码中不可用或通过 phpinfo() 可见
- android - Android BluetoothHeadset getConnectedDevices() 列表为空
- php - 从另一个相关表中的计数仅为一个的表中选择