Laravel: No access_token provided or access_token has expired

Viewed 18

I am trying to write integration test for one of my apis. So first I do is to call the passport oauth/token route to get the access_token. Which works perfect. But when I try to use the same token in the subsequent calls, it doesn't really work and returns with the following error:

{"status":{"code":401,"error":true,"error_type":"access_denied","text":"No access_token provided or access_token has expired"},"body":"Unauthenticated."}

Here is how I am doing it:

$token_body = [
'grant_type' => 'client_credentials',
'scope' => '<scope>',
'client_id' =>'<client_id>',
'client_secret' => '<secret>'
];
    
$response = $this->call('POST', '/v1/oauth/token',$token_body, [], [],  $token_headers);
$response = json_decode($response->content());
    
$access_token = $response->access_token;

After getting the token like this, I am passing this token to other route that I have to test, like this:

 $headers = [
'HTTP_Authorization' => '<abc.com>',
'accept' => 'application/json',
'accept-language' => 'en-US,en;q=0.9,und;q=0.8',
'authorization' => 'Bearer '.$access_token,
'content-type' => 'application/json',
'origin' => '<origin>',
'referer' => '<referer>',
'sec-ch-ua' => '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
'sec-ch-ua-mobile' => '?0',
'sec-ch-ua-platform' => '"Linux"',
'sec-fetch-dest' => 'empty',
'sec-fetch-mode' => 'cors',
'sec-fetch-site' => 'same-site',
'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
];

$payload = [<payload array>];

$response = $this->call('POST', '/v1/<route>', $payload, [], [],  $header);

Now what am I doing wrong here. This /v1/<route> works perfectly in the postman but here it fails.

Laravel version is 8.6 with PHP 7.4.

0 Answers
Related