我对Laravel 5.5 Passport API身份验证有一个非常奇怪的问题 .
我需要允许外部站点通过“隐式授权令牌”方法进行身份验证,并从API获取数据 .
我坚持认证 . JavaScript向API发送一个AJAX请求,但它得到的全部是401(未授权)错误(而不是令牌) .
设置是由书(https://laravel.com/docs/5.5/passport#implicit-grant-tokens)
-
Fresh Laravel 5.5安装
-
Laravel CORS添加了https://github.com/barryvdh/laravel-cors
-
Passport包安装
composer require laravel/passport
-
迁移
php artisan migrate
-
Passport安装
php artisan passport:install
-
App\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就是GET .
所有文件均可在以下网址获得:https://github.com/michalduda/laravel-passport.git
你知道什么是错的吗?