• Почему не работает авторизация по куки instagram-private-api?

    @muaddeep
    Как минимум ig.state.generateDevice(IG_USERNAME); нужно вызывать в обоих случаях.
    Я буквально вчера написал код с сохранением авторизации в файл, у меня работает:

    /**
     * Save Instagram cookies
     *
     * @param {Object} sessionObject
     * @return {Promise<void>}
     */
    const saveIGSession = async (sessionObject) => {
      return await writeFile(IG_SESSION_FILE, JSON.stringify(sessionObject));
    };
    
    
    /**
     * Load last Instagram cookies
     *
     * @return {Promise<any>}
     */
    const loadIGSession = async () => {
      let content = null;
      try {
        content = await readFile(IG_SESSION_FILE);
      } catch (e) {
        console.log('no session file');
      }
      if ( content )
        return JSON.parse(content.toString());
      return content;
    };
    
    
    /**
     * Try to log in to Instagram account
     *
     * @param {IgApiClient} ig
     * @return {Promise<null|Object>}
     * @constructor
     */
    const IGLogIn = async (ig) => {
      console.log('logging in...');
      let user = null;
      try {
        user = await ig.account.login(IG_USERNAME, IG_PASSWORD);
        console.log(user);
      } catch (e) {
        console.log(e);
      }
      return user;
    };
    
    
    /**
     * Confirm user by SMS or e-mail
     *
     * @param {IgApiClient} ig
     */
    const solveIGChallenge = async (ig) => {
      console.log(ig.state.checkpoint); // Checkpoint info here
      await ig.challenge.auto(true); // Requesting sms-code or click "It was me" button
      console.log(ig.state.checkpoint); // Challenge info here
      const {code} = await inquirer.prompt([
        {
          type: 'input',
          name: 'code',
          message: 'Enter code',
        },
      ]);
      const solvedChallenge = await ig.challenge.sendSecurityCode(code);
      console.log('solvedChallenge', solvedChallenge);
      return solvedChallenge;
    };
    
    
    /**
     * Log In and confirm if necessary
     *
     * @param ig
     * @return {Promise<Object>}
     * @constructor
     */
    const IGLogInWithChallenge = async (ig) => {
      // log in
      const loggedInUser = await IGLogIn(ig);
      if ( loggedInUser ) return loggedInUser;
      // if logging in failed request the SMS/Mail confirmation
      return await solveIGChallenge(ig);
    };
    
    
    /**
     * Find Instagram user info by its username
     *
     * @param {IgApiClient} ig
     * @param {string} nickname
     * @return {Promise<null|Object>}
     */
    const findIGUserByNickname = async (ig, nickname) => {
      let user = null;
      try {
        user = await ig.user.searchExact(nickname);
      } catch (e) {
        console.log(e);
      }
      return user;
    };
    
    
    /**
     * Subscribe to instagram-private-api events.
     * Save the session every time
     *
     * @param {IgApiClient} ig
     * @return {Promise<void>}
     */
    const subscribeToIGSessionSaving = async (ig) => {
      // This function executes after every request
      ig.request.end$.subscribe(async () => {
        const serialized = await ig.state.serialize();
        delete serialized.constants; // this deletes the version info, so you'll always use the version provided by the library
        await saveIGSession(serialized);
        console.log('session saved');
      });
    };
    
    
    /**
     * Get Instagram user info via instagram-private-api
     *
     * @param {string} nickname
     * @return {Promise<Object|null>}
     */
    const privateApiUserInfo = async (nickname) => {
    
      // start client
      const ig = new IgApiClient();
      ig.state.generateDevice(IG_USERNAME);
      // await ig.simulate.preLoginFlow();
    
      // subscribe to request events for saving the session every time
      await subscribeToIGSessionSaving(ig);
    
      // load old session
      const IGSessionOld = await loadIGSession();
    
      // set old auth data
      let loggedInWithOldSession = false;
      if (IGSessionOld) {
        // import state accepts both a string as well as an object
        // the string should be a JSON object
        await ig.state.deserialize(IGSessionOld);
        console.log('old session was loaded', IGSessionOld);
        loggedInWithOldSession = true;
      }
      // or login again
      else {
        await IGLogInWithChallenge(ig);
      }
    
      // try to get user info
      let userInfo = await findIGUserByNickname(ig, nickname);
    
      // retry if not really logged in
      if ( loggedInWithOldSession && !userInfo ) {
        // really log in again
        await IGLogInWithChallenge(ig);
        // get user info again
        userInfo = await findIGUserByNickname(ig, nickname);
      }
    
      // await ig.simulate.postLoginFlow();
    
      console.log('final', userInfo);
      return userInfo;
    };