生成Oauth客户端和通过Laravel护照中的代码请求访问令牌的问题


Issue in generating Oauth Client and requesting access token via code in Laravel Passport

我在Laravel 5.3中使用护照认证API。我已经创建了两条路由,一条用于生成oauth客户端,另一条用于请求access_token。但是我从/gen_client路由得到NULL值,并从/redirect路由显示登录页面。

有谁能帮我一下吗?

Route::get('/gen_client', function () {
    $http = new GuzzleHttp'Client();    
    $response = $http->post(url('/') . '/oauth/clients', [          
        'form_params' => [
            'id' => 'ok@test.com',
            'name' => 'OK',
            'redirect' => url('/') . '/callback'
        ],
    ]); 
    $response_body = json_decode((string)$response->getBody(), true);  
    var_dump($response_body);
}); 
Route::get('/redirect', function () {
    $oauth_client = DB::table('oauth_clients')->where('id', '=', 'ok@test.com')->first();  
    $query = http_build_query([
        'client_id' => $oauth_client->id,
        'redirect_uri' => $oauth_client->redirect,
        'response_type' => 'code',
        'scope' => '',
    ]);
    return redirect(url('/') . '/oauth/authorize?'.$query);
});
Route::post('callback', function (Request $request) {
    $http = new GuzzleHttp'Client();  
    $oauth_client = DB::table('oauth_clients')->where('id', '=', 'ok@test.com')->first();   
    $response = $http->post(url('/') . '/oauth/token', [        
        'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => $oauth_client->id,
            'client_secret' => $oauth_client->secret,
            'redirect_uri' => url('/') . '/callback',
            'code' => $request->code,
        ],
    ]);  
    $response_body = json_decode((string)$response->getBody(), true);  
    var_dump($response_body);
    $access_token = $response_body['access_token'] ;  
    $refresh_token = $response_body['refresh_token'];  
}

);

您的gen_client路由没有返回任何内容,因此它将返回NULL

您还试图引入自己的ID并直接使用oauth_clients表,这会使事情变得有些混乱。

对POST/oauth/clients的调用将返回一个负载,其中包括客户端的IDSecret

然后将这些值放入config某处,并像这样使用它们(以及您正在进行的调用所需的其他参数):

[
    'client_id' => config('services.myoauth.client_id'),
    'client_secret' => config('services.myoauth.client_secret'),
]

不要直接访问oauth_clients表,这是OAuth服务器的工作。

创建客户端(通常)是一件手工的事情,在连接OAuth客户端web应用程序到OAuth服务器时一次