Laravel API Authentication (Passport) Implicit grant markers - error 401

I have a really strange problem with Passport API authentication.

I need to allow an external site to authenticate using the Implicit Grant Token method and retrieve data from the API.

I am stuck in authentication. JavaScript sends an AJAX API request, but all it receives in return is error 401 (unauthorized) (instead of a token).

The configuration is carried out according to the book ( https://laravel.com/docs/5.5/passport#implicit-grant-tokens )

  • Fresh Laravel 5.5 install

  • Laravel CORS added https://github.com/barryvdh/laravel-cors

  • Install passport package composer require laravel/passport

  • Migration php artisan migrate

  • Passport Installation php artisan passport:install

  • App\User customized model

  • AuthServiceProvider adjusted

  • config/auth.php adjusted

  • Example client created using php artisan passport:client

  • Passport::enableImplicitGrant(); added to 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

- , ?

+4

Source: https://habr.com/ru/post/1686535/


All Articles