Keycloak Missing form parameter: grant_type

前端 未结 6 1780
攒了一身酷
攒了一身酷 2021-02-05 00:51

I have keycloak standalone running on my local machine.

I created new realm called \'spring-test\', then new client called \'login-app\'

According to the rest docu

6条回答
  •  故里飘歌
    2021-02-05 01:46

    For those who landed here from a search looking for JavaScript solution.

    Here is an example when exchanging code for access_token with keycloak authority using axios.

    querystring is used in this example:

    npm install querystring
    

    or

    yarn add querystring
    

    Sending the request:

    
    import queryString from 'querystring'
    
    const params = {
    
        grant_type: 'authorization_code,
        client_id: 'client-id-here',
        code: 'code-from-previous-redirect',
        redirect_uri: location.protocol + '//' + location.host
    
    };
    
    axios({
    
        method: 'post',
        url: 'https://my-keycloak.authority/token',
        data: queryString.stringify(params),
        config: {
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }
    
    }).then(response => {
    
        console.log(response.data);
    
    }).catch(error => {
    
        console.error(error);
    
    });
    
    

    You are required to send a POST request with the parameters as a URL encoded string in the request body.

    FormData object does not work.

提交回复
热议问题