[keycloak-user] How do I fetch a token via javascript

Harry Griff bg_ie at yahoo.com
Thu Feb 16 05:30:29 EST 2017


I'm doing a Proof of Concept and want to be automatically logged in when I start my webapp. Therefore I am trying to get my token via:
fetch("http://localhost:8082/auth/realms/mine/protocol/openid-connect/token?client_id=mine_web&username=me&password=me&grant_type=password", {
    method: "POST"
})
    .then(function (response) {
        return response.text();
    })
    .then(function (text) {
        console.log('Request successful', text.length,text);
    })
    .catch(function (error) {
        console.log('Request failed', error)
    });
But I am getting the error:POST http://localhost:8082/auth/realms/mine/protocol/openid-connect/token?client_id=mine_web&username=me&password=me&grant_type=password 400 (Bad Request)
localhost/:1 Fetch API cannot load http://localhost:8082/auth/realms/mine/protocol/openid-connect/token?client_id=mine_web&username=me&password=me&grant_type=password. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:18080' is therefore not allowed access. The response had HTTP status code 400. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
bundle.js:24422 Request failed TypeError: Failed to fetch
Chrome is also reporting: {"error":"invalid_request","error_description":"Missing form parameter: grant_type"} I tried this using curl and got the expected response:curl --data "client_id=mine_web&username=me&password=me&grant_type=password" http://localhost:8082/auth/realms/mine/protocol/openid-connect/token
How should I configure keycloak to get this to work?




More information about the keycloak-user mailing list