Search code examples
ajaxoauth-2.0axiospostmanaccess-token

Getting OAuth2 access token with jquery/axios


enter image description here

I want to get a new access token, in postman works great but when i try to do it in code, i keep getting an error in my chrome console.

with ajax:

 POST https://10.250.252.1:43002/oauth/token net::ERR_CERT_AUTHORITY_INVALID       jquery-3.3.1.min.js:2

with axios:

  POST https://10.250.252.1:43002/oauth/token net::ERR_CERT_AUTHORITY_INVALID       jquery-3.3.1.min.js:2

and

Uncaught (in promise) Error: Network Error
 at e.exports (spread.js:25)
at XMLHttpRequest.l.onerror (spread.js:25)

the code i use:

         $.ajax({
        method:"POST",
        url: `https://10.250.252.1:43002/oauth/token`,
        contentType:"application/json; charset=utf-8",
        data: {
            grant_type:"password",
            username:'user',
            password:'user',
            client_id:'11111111111111111',
            client_secret:'1111'
        }
        }).done(
            function(response) {
            console.log(response);
            });


      axios.request({
        url: "https://10.250.252.1:43002/oauth/token",
        method: "post",
        'content-type':'application/x-www-form-urlencoded',
        auth: {
          username: "user",
          password: "user"
        },
        data: {
          grant_type: "password",
          client_id:'11111111111111111',
          client_secret:'1111',
          scope: "public"
        }
      })
      .then(function(res) {
        console.log(res);
      });

Solution

  • Its SSL certificate error you need first open the URL in address bar and import the certificate to the browser.