douwei1921 2018-12-12 04:35
浏览 560

Laravel Passport总是返回401 Unauthenticated

I'm driving crazy! I've been searching around the web on thousands of StackOverFlow questions and haven't solved my issue.

I'm developing an API REST with Laravel Framework 5.7 and Passport 7.0. I've setup every file following the documentation and I'm able to get a token with grant_type=password (without authentication flow), but when I try to access to a route protected by the auth:api middleware, I always get a 401 response.

This is my HTTP request:

GET /api/usuario/1/grupos HTTP/1.1
Host: localhost:8000
Accept: application/json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI...

Here's my auth.php guard config:

'guards' => [
    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

This is the mapApiRoutes() in my RouteServiceProvider.php:

protected function mapApiRoutes()
{
    Route::prefix('api')
         ->middleware('auth:api')
         ->namespace($this->namespace)
         ->group(base_path('routes/api.php'));
}

Thanks.

This is my login request using Postman

This is the 401 when I try to use the token in an api route

EDIT:

If I disable 'auth:api' middleware, I can actually get the Bearer token using $request->bearerToken(), so the problem is not now with the CSRF token, because I can receive the request.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 关于无人驾驶的航向角
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了