Laravel Passport "auth:api" 中间件充当 "web, auth" 中间件

Laravel Passport "auth:api" middleware acts as "web, auth" middleware

我已经按照官方文档 (https://laravel.com/docs/5.3/passport#introduction) 中的描述为 Laravel 5.3 设置了 Laravel Passport 包。

我希望 API 由移动应用程序使用,因此我正在尝试实施 密码授予令牌 。我已经创建了一个密码授予客户端,并且令牌请求过程...

$response = $http->post('http://my-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => 'my@email.com',
        'password' => 'my-password',
        'scope' => '',
    ],
]);

...按预期工作,为我的一个用户返回 access-tokenrefresh-token

一方面,

php artisan route:list

列出 api/user URI 的正确中间件:api,auth:api

api 防护的驱动程序在 config/auth.php 中正确设置为 passport。 综上所述,安装过程的每一步都完成了(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 时出现问题,因为它似乎正在使用 'web' 中间件验证请求,而不是 'api'... 当我访问时,如果用户未登录,我将被重定向到 /login(登录表单),如果已登录,我将被重定向到 /home...

任何帮助将不胜感激。 提前致谢。

已解决!仅作记录,解决方案:

我用错误的 HTTP Header 向 http://my-app.com/api/user 发送了请求。我正在发送:

Type: Authorization - Content: Bearer: $accessToken 

...正确的方法是:

Type: Authorization - Content: Bearer $accessToken (without colon)

我从来没有想过这可能是一个错字...无论如何,这个错误不容易被发现,因为从一开始重定向到登录表单就误导了我。我相信这确实是一种奇怪的行为......

正确的解决方案是从此文件中删除 redirectTo()app/http/middleware/Authenticate.php

中验证中间件