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允許)
- 點擊獲取數據從服務器
- 輸入用戶名和密碼進行身份驗證的用戶
- 授權應用
- 訪問數據
很高興你已經找到它了 –