Laravel 5.6护照api身份验证无法获取请求

sal*_*war 10 php api laravel

我在做单页的应用程序,并在处理GET请求,我已经通过了X-CSRF-TOKENX-Requested-Withheaders.I还包括 \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class在网络中间件组.我在api.php中的路线看起来像

Route::get('categories','Api\Categories@index')->middleware('auth:api');
Run Code Online (Sandbox Code Playgroud)

但请求指定的URL显示未经身份验证的消息.

Pri*_*han 4

不久前我遇到了完全相同的问题。这就是我所做的修复它的方法,更多详细信息请参见这篇文章:https ://github.com/laravel/passport/issues/47

因此,这通常是为了修复 oAuth 客户端令牌:在 Passport.php 第 167 行中,到期日期设置为现在 + 100 年。

return static::$tokensExpireAt? Carbon::now()->diff(static::$tokensExpireAt): new DateInterval('P100Y');
Run Code Online (Sandbox Code Playgroud)

如果您将其设置为 P1Y,则它可以工作。就像是:

return static::$tokensExpireAt? Carbon::now()->diff(static::$tokensExpireAt): new DateInterval('P1Y');
Run Code Online (Sandbox Code Playgroud)

对于下面几行的刷新令牌也是如此:

return static::$refreshTokensExpireAt? Carbon::now()->diff(static::$refreshTokensExpireAt): new DateInterval('P1Y');
Run Code Online (Sandbox Code Playgroud)

这是针对个人令牌的:同样在 PassportServiceProvider.php 第 84 行中,关于个人令牌:

$server->enableGrantType(new PersonalAccessGrant, new DateInterval('P1Y'));

Hope it helps ! :)
Run Code Online (Sandbox Code Playgroud)