问题描述
我希望有人能解释为什么我已经unauthenticated
成功执行了Oauth 2
身份验证过程.
I hope someone could explain why I'm unauthenticated
when already has performed a successfull Oauth 2
authentication process.
我已经像Laravel的文档中一样设置了Passport
包,并且成功通过了身份验证,接收了令牌值,依此类推.但是,当我尝试对/api/user
发出请求时,例如说/api/user
,我得到一个Unauthenticated
错误作为响应.正如文档中所述,我将令牌值用作键名为Authorization
的标头.
I've set up the Passport
package like in Laravel's documentation and I successfully get authenticated, receives a token value and so on. But, when I try to do a get
request on, let say, /api/user
, I get a Unauthenticated
error as a response. I use the token value as a header with key name Authorization
, just as described in the docs.
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware("auth:api");
该功能是为了让我自己成为经过身份验证的用户,但是我只能得到Unauthenticated
.同样,如果我只返回第一个用户,我将再次得到Unauthenticated
.
This function is suppose to give back my self as the authenticated user, but I'm only getting Unauthenticated
. Likewise, if I just return the first user, I'm again getting Unauthenticated
.
Route::get('/test', function(Request $request) {
return App\User::whereId(1)->first();
})->middleware("auth:api");
在Laracast
的教程中,在指导Passport
的设置过程中,向导没有的路径中没有->middleware("auth:api")
.但是,如果它不存在,那么那就根本不需要身份验证!
In a tutorial from Laracast
, guiding through the setup of Passport
, the guider doesn't have the ->middleware("auth:api")
in his routes. But if its not there, well then there's no need for authentication at all!
请,欢迎提出任何建议或答案!
Please, any suggestions or answers are more then welcome!
推荐答案
您必须为要生成的令牌设置失效日期
You have to set an expiration date for the tokens you are generating,
将AuthServiceProvider中的启动方法设置为类似于下面的代码,然后尝试生成新令牌.护照默认有效期限返回负数
set the boot method in your AuthServiceProvider to something like the code below and try generating a new token. Passports default expiration returns a negative number
public function boot()
{
$this->registerPolicies();
Passport::routes();
Passport::tokensExpireIn(Carbon::now()->addDays(15));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
}
这篇关于护照-“未经身份验证". -Laravel 5.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!