5

正如官方文档(https://laravel.com/docs/5.3/passport#introduction)中所述,我已经为 Laravel 5.3 设置了Laravel Passport包。

我希望 API 被移动应用程序使用,所以我正在尝试实现Password Grant Tokens。我创建了一个密码授予客户端,以及令牌请求过程......

$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' => '',
    ],
]);

...按预期工作,为我的一个用户返回访问令牌刷新令牌

一方面,

php artisan route:list

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

并且 api 保护的驱动程序在 config/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时出现问题,因为它似乎是使用“web”中间件而不是“api”来验证请求......当我访问时,我如果用户未登录,则重定向到 /login(登录表单),如果是,则重定向到 /home...

任何帮助将非常感激。提前致谢。

4

2 回答 2

10

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

我正在向http://my-app.com/api/user发送请求,但 HTTP 标头错误。我正在发送:

Type: Authorization - Content: Bearer: $accessToken 

...正确的方法是:

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

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

于 2016-11-11T12:54:02.190 回答
0

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

于 2018-09-28T12:57:42.897 回答