【发布时间】: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)
全新安装 Laravel 5.5
添加了 Laravel CORS https://github.com/barryvdh/laravel-cors
护照包安装
composer require laravel/passport迁移
php artisan migrate护照安装
php artisan passport:installApp\User模型调整AuthServiceProvider调整config/auth.php调整使用
php artisan passport:client创建的示例客户端-
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
你知道出了什么问题吗?
【问题讨论】:
-
我的护照也有类似的问题。 Have a look at my question, maybe it will help you
标签: php laravel authentication http-status-code-401 laravel-passport