【问题标题】:Laravel API Authentication (Passport) Implicit Grant Tokens - 401 errorLaravel API 身份验证(护照)隐式授予令牌 - 401 错误
【发布时间】:2019-02-12 23:44:08
【问题描述】:

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

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

我卡在身份验证上。 JavaScript 向 API 发送 AJAX 请求,但得到的只是 401(未经授权)错误(而不是令牌)。

书本上的设置 (https://laravel.com/docs/5.5/passport#implicit-grant-tokens)

  1. 全新安装 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 就是它所得到的。

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

你知道出了什么问题吗?

【问题讨论】:

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


【解决方案1】:

您缺少从以下位置发布您的内容:

route\api.php

【讨论】:

    猜你喜欢
    • 2019-05-21
    • 2018-02-25
    • 2017-10-10
    • 2013-06-28
    • 2019-03-08
    • 2017-10-05
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多