passport: API Authentication Error: {"error":"invalid_client","message":"Client authentication failed"}
I followed the exact steps mentioned in the Laracast: What’s New in Laravel 5.3: Laravel Passport and API Authentication (Passport) to implement API Authentication using Oauth2.
consumer/routes/web.php
Route::get('/redirect', function () {
$query = http_build_query([
'client_id' => '3',
'redirect_uri' => 'http://consumer.dev/callback',
'response_type' => 'code',
'scope' => '',
]);
return redirect('http://passport.dev/oauth/authorize?'.$query);
});
Route::get('/callback', function (Request $request) {
$http = new GuzzleHttp\Client;
$response = $http->post('http://passport.dev/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => '3',
'client_secret' => '3TfJGj4rrvOQvjZkI8dDqx78ouH99F2DuIMKHoKH',
'redirect_uri' => 'http://consumer.dev/callback',
'code' => $request->code,
],
]);
return json_decode((string) $response->getBody(), true);
});
When I try to access http://consumer.dev/redirect, I get this error:
ClientException in RequestException.php line 111:
Client error: `POST http://passport.dev/oauth/token` resulted in a `401 Unauthorized` response:
{"error":"invalid_client","message":"Client authentication failed"}
How to resolve this? Thank you for your help!
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 35
- Comments: 51 (3 by maintainers)
@taitrongnguyen107 In your
oauth_clientstable, do the values you have above exist exactly as you have them in your database?If it doesn’t exist exactly like that in the database for
passport.devthen it will throw theinvalid_clienterror. Please be sure to check that and we will see if any further investigation is needed.make sure personal_access_client and password_client field are both 0 in table oauth_clients
Finally, I know the reason… If you wanna get the Authorization Codes, you must use the following command to generate the client…
Then you will be redirected For Authorization successfully~ Try to compare the data in their tables, you will know the reason. And you could keep on going!!!
I experienced this issue because my “redirect_uri” did not match what was in my oauth_clients table. Changing to the below code allowed me to generate the token:
@Sphinxs Check that oauth_clients.password field is set to 1. In Passport, password grant type is a special grant type that must be opted into:
php artisan passport:client --passwordhttps://laravel.com/docs/5.6/passport#password-grant-tokens
I’ve solved the issue by
below is my code.
Like @taitrongnguyen107 , I have been stuck to the same issue. Having followed the same tutorials.
I can make a
/requestcall topassport.devfromconsumer.dev. but when I clickApproveIt redirects to thecallbackroute gives me the exact same error given in the issue title.I am using passport
v2.0.4. Laravel Framework version 5.4.15.Any help would be appreciated.
@taitrongnguyen107 Ok, so I just went through the instructions and everything worked out perfectly. That leads me to believe it might be the version of Passport you have installed or the environment in which you are working. Before I post the code and data I had available for this test, I am using Homestead and have the latest version of Passport installed at the time of writing (
1.0.17). So this was the process I followed, perhaps it might show something you may have missed.I set up two different new Laravel projects and they can be accessed at
provider.devandconsumer.dev. They have two separate databases,providerandconsumer. In theproviderproject, I installed Passport withcomposer require laravel/passport. The next step was to add the service provider and run migrate the database. After that I ran thephp artisan passport:installcommand to generate the private keys and two entries in theoauth_clientstable. Next I added theHasApiTokenstrait to yourUsermodel. Then I added thePassport::routes()call to theAuthServiceProviderand changed the api guard driver fromtokentopassportinconfig/auth.php. Then I ran thephp artisan make:authcommand to scaffold out a login system for the provider as this flow requires an authenticated user. Lastly, I created a user intinkerwith the default factory provided by Laravel.Next I moved onto the
consumerproject. The first needed is to install Guzzle usingcomposer require guzzlehttp/guzzle. Next I added the two routes with the following code.With that, all of the coding necessary is done. When I visit
http://consumer.dev/redirectI get sent tohttp://provider.dev/oauth/authorize(with the query string of course). Assuming I am not logged intoprovider.devI will get sent to the/loginpage. After I login with the fake user I created earlier I get redirected back where I can either approve or deny the request. Assuming I approve it I will get redirected back tohttp://consumer.dev/callbackwith acodequery parameter that will trigger thePOSTrequest tohttp://provider.dev/oauth/tokenand return theaccess_tokenandrefresh_token.Just for completeness sake, if you deny the request, guzzle will throw a
ClientExceptionthat returns the following error from Passport.Lastly this is exactly what the
oauth_clientthat was used looks like in the database.All that being said, I don’t see a single reason besides the Passport version or your environment to be causing the problem, assuming your code and process matches up. Let me know if you see any discrepancies between your process and mine.
if you are not using the default name [Laravel Password Grant Client] please not it down and provide it during testing, i would recommend using blank if you are not sure what you you are doing. Do a php artisan migrate:rollback then php artisan migrate again, then perform a php artisan passport:keys to generate keys then lastly php artisan passport:client --password. Don’t type anything, press enter and let laravel generate the password. you should stop experiencing the error
I have the same problem but when I sent requests using Postman the same code works fine. I think the problem is, the sender and receiver both are on the localhost that is causing the problem. Otherwise there is nothing wrong in the Laravel Passport documentation or Passport version.
Another possible solution would be to check that you have your JSON configured properly. I have typed ‘client_passsword’ instead of ‘client_secret’.
I’ve solved the problem by
php artisan serve --port=8000 (for provider.dev) php artisan serve --port=8080 (for consumer.dev)
Set redirect url http://localhost:8080/callback for oauth client Don’t forget set URL’s in .env localhost:8000 for provider / localhost:8080 for consumer…
If you want servers on LAN type php artisan serve --host=192.168.?.? --port=8080 (or 8000)
Also it happens when your dev and consume site hosted locally. It is related to env variables. So you you can try paste APP_KEY value from .env file as value for config/app.php ‘key’ like this ‘key’ => “base64:l84hvor2nIytcH2BpmjWsaatLOp5hY5o9fAGEuTrYPk=”, instead of ‘key’ => env(‘APP_KEY’),
Please note that you will start receiving “invalid_request” errors automatically if upgrading to php 7.3 and switching off emulated prepares on PDO. See the discussion at thephpleague/oauth2-server#1054 for reference.
I finally figured out what was the problem. If you pass your cliend_id as a number like I was doing as shown below:
It will fail in one of Laravel`s Passport validations as you can see through this link. So in order to get it working, I only needed to add singlee (or double) quotes like this:
Oh gosh I spent 3 days to found out it was just about missing quotes. :v
please check your redirect uri as same as the url(redirect) in the table(oauth_clients),create a new client and check again,it,s work for me.