Laravel API身份验证(Passport)隐式授权令牌 - 401错误

问题描述 投票:5回答:1

我对Laravel 5.5 Passport API身份验证有一个非常奇怪的问题。

我需要允许外部站点通过“隐式授权令牌”方法进行身份验证,并从API获取数据。

我坚持认证。 JavaScript向API发送一个AJAX请求,但它得到的全部是401(未授权)错误(而不是令牌)。

设置是由书(https://laravel.com/docs/5.5/passport#implicit-grant-tokens

  1. Fresh Laravel 5.5安装
  2. Laravel CORS添加了https://github.com/barryvdh/laravel-cors
  3. 护照包安装composer require laravel/passport
  4. 迁移php artisan migrate
  5. 护照安装php artisan passport:install
  6. App\User模型调整
  7. AuthServiceProvider调整
  8. config/auth.php调整
  9. 使用php artisan passport:client 创建的示例客户端
  10. Passport::enableImplicitGrant();加入了AuthServiceProvider

JS看起来像这样:

var serialize = function(obj) {
    var str = [];
    for (var key in obj)
        if (obj.hasOwnProperty(key)) {
            str.push(encodeURIComponent(key) + "=" + encodeURIComponent(obj[key]));
        }
    return str.join("&");
}

var request = new XMLHttpRequest();
var data = {
    'client_id': '1',
    'response_type': 'token',
    'redirect_uri': 'http://localhost',
    'scope': ''
}
request.onreadystatechange = function(res) {
    if (request.readyState == XMLHttpRequest.DONE) {
        //console.log(res.responseText);
    }
}
request.open('GET', '//localhost/oauth/authorize?' + serialize(data), true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.setRequestHeader('Accept', 'application/json');

request.send();

不幸的是,401 ERROR就是GET。

所有文件都可以在:https://github.com/michalduda/laravel-passport.git上找到

你知道什么是错的吗?

php laravel authentication http-status-code-401 laravel-passport
1个回答
0
投票

您遗失了以下内容:

route\api.php
© www.soinside.com 2019 - 2024. All rights reserved.