Majeed
266
2
Laravel

How to test personal access token using Guzzlehttp in laravel

Posted 1 year ago by Majeed

I'm working on Laravel Passport. I have created a personal access token. Now, I want to test it using guzzlehttp Client. How can i do this. I'm try this way it's not working. my routes/web.php

Route::get('/callback', function (Request $request) {
         $response = (new GuzzleHttp\Client)->post('http://passportauth.test/oauth/token', [
                'form_params' => [
                    'grant_type' => 'authorization_code',
                    'client_id' => 1, // Laravel Personal Access Client ID
                    'client_secret' => 'zeFLAy76pZdCuHuKXjPCEgY0MMhnkADYbIRKzZQz', // Replace with client secret
                    'redirect_uri' => 'http://passportauth.test/callback',
                    'code' => $request->code,
                ]
            ]);

            session()->put('token', json_decode((string) $response->getBody(), true));

            return redirect('/test');
             //return json_decode((string) $response->getBody(), true);
        });

Route::get('/test', function () {
       $response = (new GuzzleHttp\Client)->get('http://passportauth.test/api/test', [
           'headers' => [
               'Authorization' => 'Bearer '.session()->get('token.access_token')
           ]
       ]);

       return json_decode((string) $response->getBody(), true);
   });

my routes\api.php


Route::get('/test',function(){
    return 'This is an API Endpoints';
})->middleware('auth:api');

Please sign in or create an account to participate in this conversation.