欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Laravel 5.3 : Passport Implementation - {“error”:“invalid_client”,“message”:“Client authentication failed”} 5.3:护照laravel实现{“错误”:“invalid_client”、“消息”:“客户端身份验证失败”}

 


I followed the exact steps mentioned in theLaracast : What's New in Laravel 5.3: Laravel Passportto implementapi authenticationusingoauth2.

Myweb.phpfile 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 toauthorizeto 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 installlaravel/passportin 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 anapiand sendpostrequest with information to be stored.

shareimprove this question
 
  
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.engvrdrSep 4 '16 at 10:28
  
I have implemented thevuecomponents 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 HarkerSep 4 '16 at 10:33

2 Answers 正确答案

 

The problem was theRedirect URLwhich I have mentioned while creating theOAuth Clientwasdifferentfrom what I needed. Following the tutorial, I had mentionedhttp://api.xyz.com/callbackwhichshouldhave beenhttp://offline.xyz.com/callback.

If you have implemented thevuecomponents, use theEditoption for theOauth Clientcreated. Change the Redirect URL appropriately.

Also, make sure theidfield and theredirectfield in theoauth-clientstable contains the same values as mentioned in the route description for/callbackin yourroutes/web.phpfile.

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

Incomposer.json, update the file with the following in therequiresection:

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

and runcomposer update.

You are good to go now.

shareimprove this answer
 
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

 

May be you're reinstall your Laravel project or reinstall the passport?Check that yourclient_idandclient_secretare 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 pasteclient_idandclient_secretfrom the "oauth_clients" datatbase table to your code for appropriate parameters (client_id, client_secret).

shareimprove this answer
 

来自  https://stackoverflow.com/questions/39315803/laravel-5-3-passport-implementation-errorinvalid-client...

我是在所述的具体步骤laracast laravel:在5.3的新的:Laravel Passport实施api authentication使用oauth2

我的web.php在客户消费项目文件看起来像这样:

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);
});

我拿到了许可请求页面,我需要authorize让我的客户访问API。但是,一旦我点击授权,我被重定向到网页上显示以下消息:

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

如何解决这个问题?

我没有装laravel/passport在脱机项目。我错过了什么吗?我必须遵循和贯彻的究竟是在视频教程中提到的。我有什么我不知道?(我有一个很基本的知识oauth2)。

如果可以,我想实现一个离线系统将定期发送数据到在线系统的时候有一个互联网连接。所以我想我可以建立一个api和发送帖子请求与信息存储

分享改善这个问题
 
  
你确定你的client_id是2?我没有看教程,但是通常的OAuth client_ids不在DB自动增加值。engvrdr16年9月4日上午10时28分
  
我已经实现了vuecomponents 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.哈里哈克16年9月4日10:33

2个答案 正确答案

问题是Redirect URL我已经提到在创建OAuth客户端不同的从我所需要的。下面的教程中,我曾提到过http://api.xyz.com/callback哪一个应该http://offline.xyz.com/callback

如果你已经实现了vue组件,使用编辑选择的Oauth Client创建.改变重定向URL适当。

同时,确保id场和重定向场中oauth-clients表包含相同的值所描述的路线/回调在你的routes/web.php文件

这应该改正错误。然而,这可能会引发另一个错误—HttpFoundationFactory没有找到

进入composer.json,在下面的更新文件要求部分:

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

和运行composer update

你是好去了

分享提高这个答案
 
 
你需要指定资助类型你试图实现。这个问题是具体到只有authorization_code资助类型和提示对安装问题。安德烈F.在10月12日16 17:22

 

可能你重装laravel项目或重装的护照吗?检查你的client_id客户_秘密是相同的在所有的地方:“环境”的文件,“oauth_clients”数据库表,并在你的代码的一部分:

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

如果这是一个不同的然后复制和粘贴client_id客户_秘密从“oauth_clients”数据库表的代码相应的参数(client_id,client_secret)。

分享提高这个答案
 

来自  https://stackoverflow.com/questions/39315803/laravel-5-3-passport-implementation-errorinvalid-client...


普通分类: