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

时间:2017-09-28 10:45:34

标签: php laravel authentication http-status-code-401 laravel-passport

我对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. Passport程序包安装composer require laravel/passport

  4. 迁移php artisan migrate

  5. Passport install php artisan passport:install

  6. App\User模型已调整

  7. AuthServiceProvider已调整

  8. config/auth.php已调整

  9. 使用php artisan passport:client

  10. 创建的示例客户端
  11. Passport::enableImplicitGrant();已添加到AuthServiceProvider
  12. 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就是GETs。

    所有文件均可在以下网址获得: https://github.com/michalduda/laravel-passport.git

    你知道出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

您错过了发布您的内容的来源:

route\api.php
相关问题