Home > front end >  Nodejs instagram-web can't save login session locally
Nodejs instagram-web can't save login session locally

Time:02-17

I've just downloaded instagram-web from https://github.com/jlobos/instagram-web-api and really enjoyed, but I have a question how can I save my instagram session on nodejs server, first the documentation says the login process should be like:

let client = new instagram({username: username, password: password});
let user = await client.login();

This methods will log my instagram in, then I can use client to call another functions, for instance, client.addComment() client.follow() ...

But there is a little problem, Instagram sometimes block the login() if I log in many times, that's why I was trying to save instagram client data as json, locally after login, to prevent this issue and avoid been blocked. So I create a function to log in and store client in a .json file, and actually I can do that process:

async function login(username, password) {
    const exist = fsUtil.existJson('client');
    if (!exist) {
        //create new client
        let client = new instagram({username: username, password: password});
        let user = await client.login();
       
        console.log(user)
        console.log(client)
        //saving client data as json
        fsUtil.createJson('client', client);
        return fsUtil.getJson('client');
    } else {
        //get client json data
        return fsUtil.getJson('client');
    }
}

Therefore, I've just created a away to store it as json, and my goal is use this client.json to make requests on instagram api without need to always log in again, reducing the change instagram notice I'm using a third party app.

The problem is when I instanciate a new client const client = new instagram({username: username, password: password});. How can I use the json data to this client I generated previously keeping the same login session and don't need to always log in the instagram account? I tried to make a reverse engenier in the source code of this npm module, but don't get it how to solve it.

CodePudding user response:

There's things you can do:

  • Store the user (and might be client) object into dictionary. With key is the hash of username password and value is the user object itself. So we can retrieve it every time we need.
  • Write your own serialize user object. If you did the reverse engineer task, then this answer will help you with the serialization.

CodePudding user response:

I've just found out, there is a away to log in by saved cookies in the machine

async function login(username, password) {
    try {
        const cookieStore = new FileCookieStore(__dirname   '\\..\\data\\cookies.json')
        const client = new instagram({username, password, cookieStore});

            let newConfig = config;
            newConfig.security.username = username;
            newConfig.security.password = password;
            fsUtil.updateConfig(newConfig);
            return "@"   username   " logado com"

    } catch (err) {
        console.log(err);
        throw new Error(err);
    }
}
  • Related