我已經爲Laravel 5.3設置了Laravel Passport包,正如官方文檔(https://laravel.com/docs/5.3/passport#introduction)中所述。Laravel Passport「auth:api」中間件充當「web,auth」中間件
我希望API可以被移動應用程序使用,所以我試圖實現密碼授權令牌。我創建了一個密碼授予客戶端,並且令牌請求過程......
$response = $http->post('http://my-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'username' => '[email protected]',
'password' => 'my-password',
'scope' => '',
],
]);
...只是正常工作,恢復爲我的用戶之一的訪問令牌和刷新令牌 。
,一方面,
php artisan route:list
列出了API /用戶URI正確中間件:API,身份驗證:API
,司機API後衛正確設置爲在配置護照 /auth.php。總結,安裝過程的每一步都已完成(https://laravel.com/docs/5.3/passport#installation)。
默認api.php的內容:
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:api');
問題是當我訪問http://my-app.com/api/user,因爲它似乎是驗證使用「網絡」的中間件,而不是「API」的請求...... 當我訪問,我被重定向到/登錄(登錄表單),如果用戶沒有登錄,並且/ home,如果它是...
任何幫助將非常感激。 在此先感謝。
你是否按照[這裏](https://laravel.com/docs/5.3/passport#consuming-your-api-with-javascript)的指示如使用X-CSRF-TOKEN來利用您自己的API端點? – dargue3
@ dargue3。是的,我還在\ Kernel.php中包含了\ Laravel \ Passport \ Http \ Middleware \ CreateFreshApiToken :: class,沒有任何改變。但是我真的不想使用JavaScript來使用我自己的API。它是關於從外部移動應用程序中使用它的,爲此我需要密碼授權類型的方法...我仍然有問題。任何其他想法? – andcl