M
M
MajorTom692018-06-01 01:53:45
JavaScript
MajorTom69, 2018-06-01 01:53:45

How to login to instagram account without using api?

You need to log in to your instagram account, emulating browser requests. I found an implementation in python and there was a desire to implement it in JavaScript too.
In the python version, request.session itself manages all cookies and headers and everything works fine:

def login(self):
        self.login_post = {
            'username': self.user_login,
            'password': self.user_password
        }

        self.s.headers.update({
            'Accept': '*/*',
            'Accept-Language': self.accept_language,
            'Accept-Encoding': 'gzip, deflate, br',
            ...
        })

        r = self.s.get(self.url)
        self.s.headers.update({'X-CSRFToken': r.cookies['csrftoken']})
        time.sleep(5 * random.random())
        login = self.s.post(
            self.url_login, data=self.login_post, allow_redirects=True)
        self.s.headers.update({'X-CSRFToken': login.cookies['csrftoken']})
        self.csrftoken = login.cookies['csrftoken']
        self.s.cookies['ig_vw'] = '1536'
        self.s.cookies['ig_pr'] = '1.25'
        self.s.cookies['ig_vh'] = '772'
        self.s.cookies['ig_or'] = 'landscape-primary'
        time.sleep(5 * random.random())

        if login.status_code == 200:
            r = self.s.get('https://www.instagram.com/')
            finder = r.text.find(self.user_login)
            if finder != -1:
                ui = UserInfo()
                self.user_id = ui.get_user_id_by_login(self.user_login)
                self.login_status = True
                log_string = '%s login success!' % (self.user_login)
                self.write_log(log_string)
            else:
                self.login_status = False
                self.write_log('Login error! Check your login data!')
        else:
            self.write_log('Login error! Connection error!')

Try with js:
var log_post = this.log_post = {
      'username': uname,
      'password': psswd
    }

    var headers = this.headers = {
      'Accept': '*/*',
      'Accept-Language': accept_language,
      'Accept-Encoding': 'gzip, deflate, br',
      ...
    }

    axios.get(url)
      .then(function(res) {
        var data = res.headers['set-cookie'];
        for (var i = 0; i < data.length; i++) {
          if (data[i].includes('csrftoken')) {
            headers['X-CSRFToken'] = data[i].split(';')[0].split('=')[1];
            console.log(headers)
          }
        }
      })
      .catch(function(err) {
        console.log(err)
      })

    setTimeout(function() {
      axios({
          method: 'post',
          url: url_login,
          data: log_post,
          headers: headers
        })
        .then(function(response) {
          console.log(response.headers['set-cookie']);
        })
        .catch(function(error) {
          console.log(error);
        });
    }, 5000)

This code returns status 200 from the post request, but authentication is unsuccessful, I really need a detailed explanation of how to implement this in JS. Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question