W
W
Webdesus2013-11-16 23:07:16
Node.js
Webdesus, 2013-11-16 23:07:16

How to make node.js authenticate on a third party resource?

Good afternoon.
There is a site written in asp to the code of which I do not have access. The programmer who maintains the resource promises to write an external service to send data. But this has been going on for a long time, and the data for conducting your analytical activities is needed now.
The problem is that there you must first enter a username and password. And I want to collect data automatically.
Authentication along the way is not basic, because I don’t see the browser storing this information in the headers after a successful login. And yes, all my attempts to do so have failed. That's why I decided to use the request module . And first send a post for authorization and then use the saved cookies for get requests. And here the problems began.
The page is used for authentication.login with which Form Data is then sent with a login and password.
Through various experiments I came up with the following code:

var postData={
    Login: 'login', PWD: 'pasword'
};
require('request').post({
    uri:"http://mysite/Login.asp",
    headers:{'content-type': 'application/x-www-form-urlencoded'},
    body:require('querystring').stringify(postData)
},function(err,res,body){
    console.log(body);
});

As a result, I started getting the following result:
<head><title>Object moved</title></head>
<body>
<h1>Object Moved</h1>This object may be found <a HREF="/Main.asp">here</a>.</body>

It seems like we entered and we were redirected to the main page and, due to the limitations of the module, he simply could not get to it, but all my get sends led back to the Login.asp page . I already generated my cookie and sent it to post and then to get .
This is where the question of the reality of my desire arises. How to do all this correctly, because I feel in my gut that it’s possible to somehow make it all easier and not explicitly transfer the username and password.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Alexandrov, 2013-11-17
@Webdesus

1. Enable cookies: var request = request.defaults({jar: true})
2. Also try adding a parameter: followAllRedirects: true
3. Why can't you add request.get(' http:/ /mysite/Main.asp ', function(err,res,body){ console.log(body); });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question