Getting 404 error when calling oAuth2 api in browser

I used ruby ​​on rails to make a RESTful api, and also use a gatekeeper for authentication and authorization. As you know, the gatekeeper generates some OAuth2 api, and the two that I need to use are / users, which are a mail request and / oauth / token, which make a token for me. Api, which I create, which publish, process, put works in the postmaster and android studio and web browser. But the post api / users and / oauth / token, which are generated by the gatekeeper, do not work in the web browser, but work fine in the android andmail studio. It confused me. The error I accept when calling this api is 404, I checked the ruby ​​mining log on the server and it says that there is no route matching. The line is that the method type and routes are correct.

This is the code I used in the reaction. I used axios:

var url="http://x.x.x.x/oauth/token";
    axios.post(url,{
        "username":"1",
        "password":"password",
        "grant_type":"password"
      },{headers:{"Content-Type":"application/json"}}).then( (response) => {
          console.log(response.data);
        })
        .catch( (error) => {
          console.log(JSON.stringify(error));
        });

And also use raw jQuery to query and get the same error. all my api work fine with two api:

var firstname = document.forms["registerForm"]["first_name"].value;
    var lastname = document.forms["registerForm"]["last_name"].value;
    var pass = document.forms["registerForm"]["password"].value;
    var passconfirm = document.forms["registerForm"]["password_confirmation"].value;


    var json_data = {
        "async": true,
        "crossDomain": true,
        "url": send,
        "method": "POST",
        "headers": {
            "content-type": "application/json",
        },
        "processData": false,
        "data":
        {
            "user": {
                "user_name": username,
                "password": pass,
                "password_confirmation": passconfirm,
                "user_type": "admin"
            },
            "profile": {
                "first_name": firstname,
                "last_name": lastname
            }
        }
    }

    $.ajax(json_data).done(function (response) {
        console.log(response);
    });
    console.log(json_data['data']);
    console.log(username);

the output of this code is console.log (JSON.stringify (error)); this:

{"config":{"transformRequest":{},"transformResponse":{},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"},"method":"post","url":"http://x.x.x.x/oauth/token.json","data":"{\"username\":\"1\",\"password\":\"password\",\"grant_type\":\"password\"}"},"request":{}}

I found the request header and response header in the browser:

Response Header:

Content-Type    
application/json; charset=UTF-8
Content-Length  
34
Connection  
keep-alive
Status  
404 Not Found
X-Request-Id    
d593b73f-eec8-41cd-95cd-e4459663358c
X-Runtime   
0.002108
Date    
Mon, 13 Nov 2017 11:19:26 GMT
X-Powered-By    
Phusion Passenger 5.1.11
Server  
nginx/1.12.1 + Phusion Passenger 5.1.11


Request headers (427 B) 
Host    
x.x.x.x
User-Agent  
Mozilla/5.0 (X11; Ubuntu; Linu…) Gecko/20100101 Firefox/56.0
Accept  
text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Language 
en-US,en;q=0.5
Accept-Encoding 
gzip, deflate
Access-Control-Request-Method   
POST
Access-Control-Request-Headers  
Origin  
http://localhost:3000
Connection  
keep-alive
+4
source share
4 answers

This seems to be due to the CORS policy. You can rack the cores . This is the middleware for the rack and you can send cross-domain requests to your gatekeeper-protected application

+1
source

After checking the docs , it looks like you need to change the header Content-Typeto application/x-www-form-urlencoded, and the data to key=valuepair:

const data = 'username=1&password=password&grant_type=password'

or simply:

const formData = {
  username: '1',
  password: 'password', 
  grant_type: 'password',
}

const data = Object.keys(formData)
  .map(prop => `${prop}=${formData[prop]}`)
  .join('&')

:

var url="http://x.x.x.x/oauth/token";

axios.post(url, data, {
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  }
}).then( (response) => {
  console.log(response.data);
})
.catch( (error) => {
  console.log(JSON.stringify(error));
});
+1

You need to configure OAuth settings to allow access to the client web application. Check the logs or error message you get in api response.

0
source
    function login(username, password, returnUrl) {

    const formData = {
        grant_type:"password",
        client_id:"xxxxxxxx",
        client_secret: "xxxxxxxxx",
        redirect_uri:"xxxxxxxx.com/api/v1/accounts/profile/",
        username:username,
        password:password
    }

    const d = Object.keys(formData)
      .map(prop => '${prop}=${formData[prop]}')
      .join('&')

    const requestOptions = {
        method: 'POST',
        headers: { 
            "Content-Type": "application/x-www-form-urlencoded",
            'Accept': 'application/json'
         },
        body: d
    };

    return fetch('xxx.com/o/token/', requestOptions)
        .then(handlePostResponse)
        .then(token => {

            // login successful if there a user in the response
            if (token) {
                token.token = token
            }

            console.log(token)

            return token;
        });

}
0
source

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


All Articles