instagram-private-api: My session function doesn't seem to work, what's the wrong?

General Question

Read the Notes and fill out the form. Switch to Preview for reading.

Notes

Your issue will be closed if you violate any rule below.

  • You need to include a meaningful descriptions and e.g. a screenshot.
  • You do not include screenshots of your code.
  • Read the Docs.
  • Capturing endpoints and similar can be found here (read it if you are going to ask about it).
  • You can delete this section after reading it.

Form

Put an [x] if you meet the condition, else leave [ ].

Question

A specific question, so it’s understandable to anyone. You may add pictures.

YOUR QUESTION HERE I was working on this function, it’s bassed on the given example in docs:

require("dotenv").config();
const fs = require('fs');
const { IgApiClient } = require("instagram-private-api");
const ig = new IgApiClient();
const username = process.env.IG_USERNAME;
const password = process.env.IG_PASSWORD;
ig.state.generateDevice(username);

const dataCheck = function (callback) {
  fs.readFile('login_data.json', function (err, data) {
    console.log(data);
    if (data.length > 0) {
      callback(true);
    } else {
      callback(false);
    }
  });
}

const dataLoad = function (callback) {
  fs.readFile('login_data.json', function (err, data) {
      console.log("Read JSON file: " + data);
      data = JSON.parse(JSON.stringify(data));
      console.log("Object: " + data);
      callback(data);
  });
}

function dataSave(data){
  console.log("saving...");
  fs.writeFile("login_data.json", JSON.stringify(data),    function(err) {
    if(err) {
      console.log(err);
    }
    console.log("The file was saved!");
  }); 
}

igLogin();

async function igLogin(){

  await ig.simulate.preLoginFlow();

  ig.request.end$.subscribe(async () => {
    const serialized = await ig.state.serialize();
    delete serialized.constants;
    dataSave(serialized);
  });
  
  dataCheck(async dataExists => {
    console.log(dataExists);
    if (dataExists){
      dataLoad(async data => {
        ig.state.deserialize(data);
      }); 
    }
  });

  const auth = await ig.account.login(username, password);
  console.log(auth);
  
  process.nextTick(async () => await ig.simulate.postLoginFlow());

}

It can save the login data as a json file, It can login, but on the Instagram login activity it appears as a new login activity and not an old session. So, where is the mistake?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 35

Most upvoted comments

Hi, I had the same issues that load session returns Cookie "ds_user_id" not found. I found it because I changed the API version in core/constants.js; it won’t return the cookies in session. Replace APP_VERSION with “121.0.0.29.119” and APP_VERSION_CODE with “185203708” and create a session again. I hope this helps you guys.

@SaliZumberi Please paste your debug output here inside of code tags.

Shouldnt you save session after login as well? If you do not have a cookie @Nerixyz

Adding this will ensure the session is always saved:

ig.request.end$.subscribe(async () => save(await ig.state.serialize()));

I just saw how you have to change the try catch block:

    if (exists()) {
        await ig.state.deserialize(...);
        await ig.user.info(ig.state.cookieUserId);
        shouldLogin = false;
    } else {
      await ig.simulate.preLoginFlow();
    }

This way you only do this before logging in. But I just tested the code and it works fine. Your cookie not found error should not show up because it’s ignored by the empty catch block.

This isn’t perfect but it will work

// create ig, generate device
ig.request.end$.subscribe(async () => {
    const serialized = await ig.state.serialize();
    delete serialized.constants;
    save(serialized);
});

let shouldLogin = true;

try {
    await ig.simulate.preLoginFlow();
    if (exists()) {
        await ig.state.deserialize(...);
        await ig.user.info(ig.state.cookieUserId);
        shouldLogin = false;
    }
} catch (_) { }

if (shouldLogin) {
    await ig.account.login(...);
}

This not simple by the way…

Change

 const fs = require('fs'); 

to

const { promises: fs } = require('fs');

and you’re done.

You don’t have to log in after loading the state. But you have to be careful in your code because you’re mixing up callbacks and async/await. Try to use one concept throughout your project.