have you try using this api token and send the same request from postman ?
How to authenticate when consume api in Laravel?
I'm trying to integate api service into my laravel web app. From login & every operation in my web app is using the api. But I always get response 401 unauthorized whenever I hit the api endpoint.
First of all I logged in using this request to api, I can successfully got the response.
public function login($user_id, $password)
{
try {
$guzzle = new Client();
$url = config('app.base_api_url') . 'auth/signin';
$credentials = base64_encode($user_id.':'.$password);
$response = $guzzle->post($url, [
'Authorization' => ['Basic '.$credentials],
'form_params' => [
'userid' => $user_id,
'password' => $password,
],
]);
return json_decode($response->getBody());
} catch (\Exception $e) {
throw $e;
}
}
The response from api above is like this:
{
"user": {
"status": "success",
"message": "user successfully loggedin",
"payload": {
"userid": "user001",
"role": "R.5",
"name": "User",
"title": "",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJzZXJ0aWZpa2FzaWhhbGFsaXBiQGdtYWlsLmNvbSIsIm5hbWUiOiJMUEggSVBCIiwiaWF0IjoxNjU2MzgzMTExLCJleHAiOjE2NTYzODMxNDEsImF1ZCI6InNpaGFsYWwuYnBqcGguZ28uaWQiLCJpc3MiOiJzaWhhbGFsLmJwanBoLmdvLmlkIn0.vfceMwXAAMoTlcrGZHLn72y_sg5J1Rz4_SFmhwOiGTc",
"refreshToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJzZXJ0aWZpa2FzaWhhbGFsaXBiQGdtYWlsLmNvbSIsIm5hbWUiOiJMUEggSVBCIiwiaWF0IjoxNjU2MzgzMTExLCJleHAiOjE2NTY5ODc5MTEsImF1ZCI6InNpaGFsYWwuYnBqcGguZ28uaWQiLCJpc3MiOiJzaWhhbGFsLmJwanBoLmdvLmlkIn0.Wa06POKEvjKoN28CPHjTiDahUSMvAc_2PRrIMpv9NBg"
}
}
}
And then I want to get the data from api, but I always got 401 Unauthorized, Error: You are not logged in response. (I hard coded the token only for development purpose)
public function getCost($page, $order_dir, $limit)
{
try {
$guzzle = new Client();
$url = config('app.base_api_url') . 'api/v1/costs';
$token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOiJzZXJ0aWZpa2FzaWhhbGFsaXBiQGdtYWlsLmNvbSIsIm5hbWUiOiJMUEggSVBCIiwiaWF0IjoxNjU2MzgzMTExLCJleHAiOjE2NTYzODMxNDEsImF1ZCI6InNpaGFsYWwuYnBqcGguZ28uaWQiLCJpc3MiOiJzaWhhbGFsLmJwanBoLmdvLmlkIn0.vfceMwXAAMoTlcrGZHLn72y_sg5J1Rz4_SFmhwOiGTc";
$response = $guzzle->get($url, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
'form_params' => [
'page' => $page,
'order_dir' => $order_dir,
'limit' => $limit,
],
]);
return json_decode($response->getBody());
} catch (\Exception $e) {
throw $e;
}
}
I don't know what's wrong with my code. Did I have to logged in the user manually or something like that? I'm stuck with this since yesterday. Any help would be very helpful for me
Please or to participate in this conversation.