0
我正在开发第三方应用程序可以从Laravel服务器访问数据的项目。我也在laravel中创建了一个客户端应用程序进行测试。Laravel Passport在授权之前验证用户
以下代码要求授权并且其工作正常。
Route::get('/applyonline', function() {
$query = http_build_query([
'client_id' => 5,
'redirect_uri' => 'http://client.app/callback',
'response_type' => 'code',
'scope' => '',
]);
return redirect('http://server.app/oauth/authorize?'.$query);
});
如何在授权之前验证用户身份?现在我可以使用此代码访问数据表单服务器。
Route::get('/callback', function (Request $request) {
$http = new GuzzleHttp\Client;
$response = $http->post('http://server.app/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 2,
'client_secret' => 'fcMKQc11SwDUdP1f8ioUf8OJwzIOxuF8b2VKZyip',
'username'=> '[email protected]',
'password' => 'password',
],
]);
$data = json_decode((string) $response->getBody(), true);
$access_token = 'Bearer '. $data['access_token'];
$response = $http->get('http://server.app/api/user', [
'headers' => [
'Authorization' => $access_token
]
]);
$applicant = json_decode((string) $response->getBody(), true);
return view('display.index',compact('applicant'));
});
虽然上面的代码工作正常,但我不认为它是一个很好的方式来问客户端的用户名和密码。
我想用这个流程(同Facebook允许)
- 点击获取数据从服务器
- 输入用户名和密码进行身份验证的用户
- 授权应用
- 访问数据
很高兴你已经找到它了 –