0

I'm trying to use the Node module Request to authenticate on a website, and after following instructions I still can't seem to figure it out...

Using Coffeescript I've done the following:

Request {
    url: "https://#{encodeURIComponent(config.email)}:#{encodeURIComponent(config.password)}@#{config.loginURL}"
}, (error, response, body) ->
    if error
        console.log error
    else
        console.log body

The config values are, respectively, an email, password, and the URL. The URL is app.shopify.com/services/partners/auth/login. Also, because the username is an email address, I thought it be best to encodeURIComponent the login values.

When I run this I don't get an error, but the output of the body is just the markup of the login page.

I've also tried doing it like this:

Request.get config.loginURL, {
    auth: {
        username: config.email
        password: config.password
        sendImmediately: false
    }
}, (error, response, body) ->
    if error
        console.log error
    else
        console.log body

In this case, because the credentials aren't part of the URL, I haven't encodeURIComponent'd them. Also, the login URL has https:// prepended to it.

Can anyone guide me in the right direction?

Jody Heavener
  • 2,704
  • 5
  • 41
  • 70
  • What happened with the second case (is `error` set? what is `response.statusCode`?)? Did you try with `sendImmediately: true`? Did you check `config.email` and `config.password` to make sure they are valid (at least not empty)? – mscdex Oct 24 '14 at 21:30
  • @mscdex setting `sendImmediately` to `true` didn't seem to do anything (though I'm not sure what "Digest authentication" is), I can confirm that the config values are set and Strings, and the response codes from both cases are 200. So I _think_ everything is set correctly? – Jody Heavener Oct 24 '14 at 21:57

1 Answers1

0

If the page is not using basic/digest authentication and is instead using form-based authentication, you need to POST form values instead:

request(config.loginURL, {
  method: 'POST',
  form: {
    // these key names come from the `name` field in the login form's HTML
    login: config.email,
    password: config.password
  }
}, function(err, res, body) {
  // ...
});
mscdex
  • 104,356
  • 15
  • 192
  • 153