[英]Laravel Passport API call always return Unauthenticated
所以我已经用 Passport 设置了我的 API,并尝试发出 GET 请求将近一个星期,但仍然得到以下响应:
{
"message": "Unauthenticated."
}
以下是我的配置:
'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);
}
它返回我在请求中使用的 access_token 。 我尝试了下面列出的所有解决方案,但都没有奏效:
# 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;
}
请帮忙,我现在很绝望:)
问题在于您如何进行身份验证以及您使用的是哪个令牌。
有两种生成token的方法
现在,在 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');
请记住,如果您使用客户端身份验证,则必须在App/http/kernel.php
routemiddleware
中添加以下行
'client_credentials' => \\Laravel\\Passport\\Http\\Middleware\\CheckClientCredentials::class
我希望能解决问题
尝试使用以下命令创建新的密码授予客户端:
php artisan passport:client --password
你会得到如下输出:
What should we name the password grant client? [Laravel Password Grant Client]:
>
Password grant client created successfully.
Client ID: 4
Client secret: WlRYEbA5lt5esbi0MuFyJPzPDmHDGsk3iC5QGw7d
使用这些凭据填写您的客户端 ID 和密码。 通过 Vue 组件接口创建的标准客户端凭据不适用于密码授予。
尝试在 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,
],
];
或者在 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.