我一整天都找到了很多教程。我的设置与所有基本教程完全相同。
目前,我可以访问http://localhost/oauth/token
成功地将令牌返回给我。
之后,我使用 ARC(Advanced Rest Client)来进行调用我自己的 api 的测试。
我已经通过了标题,例如
Authorization: Bearer the_token_here
accept: application/json
从该标头中,我只想访问 laravel 提供的默认 API/user
.
但是,我总是得到这样的回应{ "message": "Unauthenticated." }
参考这个教程https://itsolutionstuff.com/post/laravel-5-how-to-create-api-authentication-using-passport-example.html https://itsolutionstuff.com/post/laravel-5-how-to-create-api-authentication-using-passport-example.html
我可以按照教程登录,但无法通过端点获取数据details
。它返回的响应{ "message": "Unauthenticated." }
我的路线api.php
Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function(){
Route::get('/user', function( Request $request ){
return $request->user();
});
});
顺便说一句,laravel.log 中没有错误消息,并且我已设置为调试模式
更新感谢 Mayank 的评论指出
League\\OAuth2\\Server\\Exception\\OAuthServerException: The resource owner or authorization server denied the request. in /.../vendor/league/oauth2-server/src/Exception/OAuthServerException.php:173
Stack trace:
#0 /.../vendor/league/oauth2-server/src/AuthorizationValidators/BearerTokenValidator.php(59): League\\OAuth2\\Server\\Exception\\OAuthServerException::accessDenied('Missing "Author...')
#1 /.../vendor/league/oauth2-server/src/ResourceServer.php(82): League\\OAuth2\\Server\\AuthorizationValidators\\BearerTokenValidator->validateAuthorization(Object(Zend\\Diactoros\\ServerRequest))
#2 /.../vendor/laravel/passport/src/Http/Middleware/CheckClientCredentials.php(46): League\\OAuth2\\Server\\ResourceServer->validateAuthenticatedRequest(Object(Zend\\Diactoros\\ServerRequest))