instagram-private-api icon indicating copy to clipboard operation
instagram-private-api copied to clipboard

Recommendations and advice for proxying

Open imansyed0 opened this issue 1 year ago • 8 comments

General Question

Form

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

Question

I've set up Oxylabs residential proxying with session logic to reuse a given server at the geolocated country of the user if they try to log in more than once, but unfortunately Instagram is still flagging the logins as 'unusual' every time. Even after the user goes on Instagram and declares it was them that did the login, reattempting brings up the same error again, even though the proxy setup does then use the same server again for the rerequest. Anyone have advice on how to stop getting flagged as suspicious? Would ISP proxies help?

image

Code

async function loginToInstagram(username, password, verificationCode, challengeChoice, proxyUrl) {
  const ig = new IgApiClient();
  ig.state.generateDevice(username);

  const proxyAgent = new HttpsProxyAgent(proxyUrl);
  ig.request.defaults.agent = proxyAgent;

  let clientInfo = await loadClientInfo(username);
  if (!clientInfo) {
    clientInfo = {
      deviceId: ig.state.deviceId,
      phoneId: ig.state.phoneId,
      uuid: ig.state.uuid,
      adid: ig.state.adid,
      build: ig.state.build,
    };
    await saveClientInfo(username, clientInfo);
  } else {
    ig.state.deviceId = clientInfo.deviceId;
    ig.state.phoneId = clientInfo.phoneId;
    ig.state.uuid = clientInfo.uuid;
    ig.state.adid = clientInfo.adid;
    ig.state.build = clientInfo.build;
  }

  const cookieLoaded = await loadCookie(ig, username);
  if (cookieLoaded) {
    console.log('Cookies loaded, checking if session is valid...');
    try {
      if (await isLoggedIn(ig)) {
        console.log('Using existing valid session');
        return { ig, status: 'LOGGED_IN' };
      } else {
        console.log('Loaded session is no longer valid');
      }
    } catch (error) {
      console.log('Error checking login status:', error.message);
    }
  } else {
    console.log('No existing cookies found');
  }

  console.log('Proceeding with new login...');
  try {
    await ig.simulate.preLoginFlow();
    const loggedInUser = await ig.account.login(username, password);
    process.nextTick(async () => await ig.simulate.postLoginFlow());
    await saveCookie(ig, username);
    console.log('New login successful');
    return { ig, status: 'LOGGED_IN' };
  } catch (error) {
    if (error instanceof IgCheckpointError) {
      console.log('Checkpoint error occurred');
      ig.state.challengeUrl = error.response.body.challenge.url;
      
      await resetChallenge(ig);

      if (!challengeChoice) {
        const challengeInfo = await getChallengeChoices(ig);
        return { status: 'CHALLENGE_REQUIRED', challengeInfo };
      } else if (!verificationCode) {
        await ig.challenge.selectVerifyMethod(challengeChoice);
        return { status: 'VERIFICATION_CODE_NEEDED' };
      } else {
        const { body } = await ig.challenge.sendSecurityCode(verificationCode);
        if (body.logged_in_user) {
          await saveCookie(ig, username);
          return { ig, status: 'LOGGED_IN' };
        } else {
          throw new Error('Failed to solve challenge');
        }
      }
    }
    console.error('Login error:', error);
    throw error;
  }
}

imansyed0 avatar Jul 11 '24 17:07 imansyed0

Having same issues i don't think its a proxy issue i cant even login just getting that 400 error

wiz64 avatar Jul 14 '24 15:07 wiz64

when i use deserealize i get this error, else just works

wiz64 avatar Jul 14 '24 16:07 wiz64

select verify method returns[Object: null prototype] { action: 'close', status: 'ok' } seems like the api needs to be updated, even if i somehow pass this stage i get flagged while scraping

wiz64 avatar Jul 14 '24 17:07 wiz64

It's because you changed device data. Not because of proxy. If proxy has issues you will need OTP. The challenge API is outdated in this library. So you can't pass challenge here.

sunmasters avatar Jul 15 '24 02:07 sunmasters

It's because you changed device data. Not because of proxy. If proxy has issues you will need OTP. The challenge API is outdated in this library. So you can't pass challenge here.

Nah device data is same but i am also getting an otp challenge I can't pass otp agreed, what's the solution to challenge?

Is there anyone who has done it already or let's do it !?

wiz64 avatar Jul 15 '24 10:07 wiz64

I have the same issue. An update for the challenge handler of this library would be great

martincanavate avatar Oct 29 '24 13:10 martincanavate

did anyone manage to solve it?

martincanavate avatar Oct 29 '24 13:10 martincanavate

Yes, can be solved

sunmasters avatar Oct 29 '24 17:10 sunmasters