总是得到"消息":"未经身份验证。" - Laravel Passport

时间:2018-03-23 08:46:36

标签: php laravel laravel-passport

我整整都找到了很多教程。我的设置与所有基本教程完全相同。

目前,我已成功返回http://localhost/oauth/token令牌给我。

之后,我使用ARC(Advanced Rest Client)来测试我自己的api。

我已经通过了诸如

之类的标题
Authorization: Bearer the_token_here
accept: application/json

从该标题中,我只想访问laravel /user提供的默认API。

但是,我总是收到{ "message": "Unauthenticated." }

的回复

参考本教程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中没有错误消息,我已经设置为Debug模式

更新感谢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))

5 个答案:

答案 0 :(得分:4)

要获取原因的详细错误消息,您需要转到CheckClientCredentials类详细信息,如下所示

public function handle($request, Closure $next, ...$scopes)
{
    $psr = (new DiactorosFactory)->createRequest($request);

    try {
        $psr = $this->server->validateAuthenticatedRequest($psr);
    } catch (OAuthServerException $e) {
        error_log($e->getHint()); // add this line to know the actual error
        throw new AuthenticationException;
    }

    $this->validateScopes($psr, $scopes);

    return $next($request);
}

基于错误消息。在我的问题中。

解决方案是将其添加到根文件夹的.htaccess(不仅仅是在公共文件夹中)

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

官方文件refer here

中还有一张纸条

如果没有上述配置,从任何地方到应用程序的调用期间都会忽略Authorization标头。一旦被忽略,内部类将无法检索此标头数据

答案 1 :(得分:1)

在 Ubuntu 中,执行以下操作。

启用重写模式。

sudo a2enmod rewrite

转到cd /etc/apache2

然后打开apache2.conf nano apache2.conf,找到下面这行,将AllowOverride None改为AllowOverride All,如下图.

# /etc/apache2/apache2.conf
<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

最后重启apache2服务器

sudo service apache2 restart

答案 2 :(得分:0)

万一有人遇到相同的问题,而所选解决方案不能解决问题。检查以下内容:

1)检查您是否在请求的标题中发送了X-CSRF-TOKEN。就我而言,我将vue与axios结合使用:

let token = window.$('meta[name="csrf-token"]').attr('content');
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token;

如果要发送,请尝试在 vendor / laravel / passport / src / Passport.php 第125行中更改以下值(可能会更改)

正确错误

public static $unserializesCookies = false;

问题可能类似于https://github.com/laravel/passport/issues/452中的问题

有关序列化的说明在其中

答案 3 :(得分:0)

如果您尝试了所有操作,但似乎无济于事,请尝试清除配置缓存。我花了两天时间重新安装通行证,遵循了十亿个教程,创建了测试项目等,所有这些最终使我意识到我需要清除缓存

php artisan config:cache

答案 4 :(得分:0)

将以下代码粘贴到项目根文件夹中的.htaccess文件中。

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1