[英]Laravel Passport API call always return Unauthenticated
So I have setup my API with Passport and tried to make GET request for almost a week now but still getting the response bellow :所以我已经用 Passport 设置了我的 API,并尝试发出 GET 请求将近一个星期,但仍然得到以下响应:
{
"message": "Unauthenticated."
}
Below are my configuration :以下是我的配置:
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'token',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
//'hash' => true,
],
],
public function boot()
{
$this->registerPolicies();
//
Passport::routes();
Passport::tokensExpireIn(Carbon::now()->addDays(7));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(14));
}
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('auth:api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
public function callback(Request $request)
{
$http = new Client();
$token_url=url('oauth/token');
$response = $http->post($token_url, [
'form_params' => [
'grant_type' => 'password',
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'username'=>'my-username',
'password'=>'my-password',
'scope' =>'*',
],
]);
return json_decode((string) $response->getBody(), true);
}
Which returns an access_token that I use in my request in my request .它返回我在请求中使用的 access_token 。 I tried all the solution listed below and none of them worked :我尝试了下面列出的所有解决方案,但都没有奏效:
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
public static function tokensExpireIn(DateTimeInterface $date = null)
{
if (is_null($date)) {
return static::$tokensExpireAt
? Carbon::now()->diff(static::$tokensExpireAt)
: new DateInterval('P1Y');
}
static::$tokensExpireAt = $date;
return new static;
}
Please help , I'm desperate now :)请帮忙,我现在很绝望:)
The problem is how you authenticate and which token you are using.问题在于您如何进行身份验证以及您使用的是哪个令牌。
There are 2 ways to generate token有两种生成token的方法
Now in the API routes you have to tell in your method how you are authenticating for example现在,在 API 路由中,您必须在方法中说明您是如何进行身份验证的,例如
// Below one is for authenticating Client token
Route::get('/test', 'Api\TestController@index')->middleware('client_credentials');
// Below one is for the User token
Route::get('/test', 'Api\TestController@index')->middleware('auth:api');
And Remember if you are using client authentication, you have to add the below line in routemiddleware
in App/http/kernel.php
请记住,如果您使用客户端身份验证,则必须在App/http/kernel.php
routemiddleware
中添加以下行
'client_credentials' => \\Laravel\\Passport\\Http\\Middleware\\CheckClientCredentials::class
I hope that solves issues我希望能解决问题
Try creating a new password grant client with:尝试使用以下命令创建新的密码授予客户端:
php artisan passport:client --password
You'll get an output like:你会得到如下输出:
What should we name the password grant client? [Laravel Password Grant Client]:
>
Password grant client created successfully.
Client ID: 4
Client secret: WlRYEbA5lt5esbi0MuFyJPzPDmHDGsk3iC5QGw7d
Use those credentials to fill your client id and secret.使用这些凭据填写您的客户端 ID 和密码。 Standard client credentials created through the Vue component interface do not work for password grants.通过 Vue 组件接口创建的标准客户端凭据不适用于密码授予。
try to comment middlewares in api middleware group (app/HTTP/Kernel.php)尝试在 api 中间件组 (app/HTTP/Kernel.php) 中评论中间件
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
],
'api' => [
// 'throttle:60,1',
// \Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
Or comment api middleware group in app/Providers/RouteServiceProvider.php或者在 app/Providers/RouteServiceProvider.php 中注释 api 中间件组
protected function mapApiRoutes()
{
Route::prefix('api')
// ->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.