admin管理员组文章数量:1245095
I try to call api in laravel 8 in it another bank api call when token is expire so some getting error below menstion
GuzzleHttp\Exception\ClientException: Client error: POST https://bankul/acctenq/v2/prefetchAccount
resulted in a 401 Unauthorized
response:
{ "error" : "Token has been revoked." }
in file C:\wamp64\www\manali\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113
so how can handle it any idea for that
any idea for thar error
I try to call api in laravel 8 in it another bank api call when token is expire so some getting error below menstion
GuzzleHttp\Exception\ClientException: Client error: POST https://bankul/acctenq/v2/prefetchAccount
resulted in a 401 Unauthorized
response:
{ "error" : "Token has been revoked." }
in file C:\wamp64\www\manali\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113
so how can handle it any idea for that
any idea for thar error
Share Improve this question edited Feb 15 at 16:51 VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked Feb 15 at 10:06 AshokAshok 12 bronze badges 2- As your token is not valid, error 401 is a perfect error-hint. Why would you expect something different? – gratinierer Commented Feb 16 at 20:29
- I am not getting proper response json as it is show here or not, similarly i am getting the error line showing there – Ashok Commented Feb 19 at 4:05
1 Answer
Reset to default 0The error message indicates that your API request is failing due to an expired or revoked token (401 Unauthorized). This typically happens when the authentication token used for the request is no longer valid.
You need to implement token refreshing in your Laravel application
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
function fetchAccountData()
{
$client = new Client();
$token = getToken(); // Function to get the stored token
try {
$response = $client->post('https://bankul/acctenq/v2/prefetchAccount', [
'headers' => [
'Authorization' => "Bearer $token",
'Accept' => 'application/json',
],
'json' => [
// Your request body here
],
]);
return json_decode($response->getBody(), true);
} catch (ClientException $e) {
// Check if error is due to token expiration
if ($e->getCode() === 401) {
refreshAccessToken(); // Call function to refresh token
return fetchAccountData(); // Retry the request
}
throw $e; // If it's a different error, throw it
}
}
Implement Token Refresh Function
function refreshAccessToken()
{
$client = new Client();
$response = $client->post('https://bankul/oauth/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'client_id' => env('BANK_CLIENT_ID'),
'client_secret' => env('BANK_CLIENT_SECRET'),
'refresh_token' => getRefreshToken(),
],
]);
$data = json_decode($response->getBody(), true);
storeToken($data['access_token'], $data['refresh_token']); // Store new tokens
}
Store and Retrieve Tokens
function storeToken($accessToken, $refreshToken)
{
session(['bank_access_token' => $accessToken, 'bank_refresh_token' => $refreshToken]);
}
function getToken()
{
return session('bank_access_token');
}
function getRefreshToken()
{
return session('bank_refresh_token');
}
If the API does not provide a refresh token, you may need to request a new access token from scratch. Ensure you handle API rate limits when retrying requests. Use Laravel’s built-in caching or database to store tokens instead of session storage for better persistence. This approach ensures your Laravel app automatically refreshes the token and retries the request without manual intervention.
本文标签: Api call in another third party api call using guzzlehttp in laravel 8Stack Overflow
版权声明:本文标题:Api call in another third party api call using guzzlehttp in laravel 8 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740243295a2247771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论