9

I followed the exact steps mentioned in the Laracast : What's New in Laravel 5.3: Laravel Passport to implement api authentication using oauth2.

My web.php file in the client/consumer project looks like:

use Illuminate\Http\Request;


Route::get('/', function () {
$query = http_build_query([
     'client_id' => 2,
     'redirect_uri' => 'http://offline.xyz.com/callback',
     'response_type' => 'code',
     'scope' => '',
    ]);

return redirect ('http://api.xyz.com/oauth/authorize?'.$query);
});

Route::get('/callback', function (Request $request){
$http= new GuzzleHttp\Client;

$response = $http->post('http://api.xyz.com/oauth/token',[
    'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => 2 , 
            'client_secret' => 'tUGYrNeWCGAQt220n88CGoXVu7TRDyZ20fxAlFcL' ,
            'redirect_uri' => 'http://offline.xyz.com/callback',
            'code' => $request->code,
        ],
    ]);

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

I am getting the permission request page where I need to authorize to allow my client to access the api. But, once I click authorize, I am being redirected to the page where it shows the following message:

{"error":"invalid_client","message":"Client authentication failed"}

How to resolve this?

I did not install laravel/passport in the offline project. Am I missing out something? I have followed and implemented what exactly was mentioned in the video tutorial. Do I have to include something else that I'm not aware of? (I have a very basic knowledge on oauth2).

If it helps, I am trying to implement an offline system which will periodically send data to an online system when there is an internet connection. So I thought I can build an api and send post request with information to be stored.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Hari Harker
  • 702
  • 1
  • 12
  • 29
  • Are you sure your client_id is 2? I didn't watch the tutorial, but usually the oauth client_ids are not the auto increment values in db. – engvrdr Sep 04 '16 at 10:28
  • I have implemented the `vue` components as well and there I can see the list of clients I have with their ids. And with that, I am sure I have a client with id = 2. – Hari Harker Sep 04 '16 at 10:33

2 Answers2

4

The problem was the Redirect URL which I have mentioned while creating the OAuth Client was different from what I needed. Following the tutorial, I had mentioned http://api.xyz.com/callback which should have been http://offline.xyz.com/callback.

If you have implemented the vue components, use the Edit option for the Oauth Client created. Change the Redirect URL appropriately.

Also, make sure the id field and the redirect field in the oauth-clients table contains the same values as mentioned in the route description for /callback in your routes/web.php file.

This should fix the error. However, it might raise another error - HttpFoundationFactory not found.

In composer.json, update the file with the following in the requiresection:

"symfony/psr-http-message-bridge": "0.2"

and run composer update.

You are good to go now.

Hari Harker
  • 702
  • 1
  • 12
  • 29
  • 1
    You need to specify which Grant Type your attempting to implement. This fix is specific to ONLY the authorization_code Grant type and hints more towards a setup issue. – Andre F. Oct 12 '16 at 17:22
  • changing the redirect_url fixed it for me – George Jun 18 '19 at 09:47
  • What do you mean with "If you have implemented the vue components, use the Edit option for the Oauth Client created. Change the Redirect URL appropriately." I have the problem where Vue redirects me, but the URL is not updating, and all is inside a weird box – Carlos Tinnelly Mar 09 '21 at 23:36
  • 1
    @carlos I meant the UI that Laravel ships with to create and manage oauth clients. They are vue components. – Hari Harker Mar 10 '21 at 19:42
2

May be you're reinstall your Laravel project or reinstall the passport? Check that your client_id and client_secret are the same in all places: ".env" file, "oauth_clients" database table, and in your part of code:

   'client_id' => ....,
   'client-secret' => ......

In case if it's a different then copy and paste client_id and client_secret from the "oauth_clients" datatbase table to your code for appropriate parameters (client_id, client_secret).

Alex
  • 1,297
  • 1
  • 16
  • 12