dilame / instagram-private-api

NodeJS Instagram private API SDK. Written in TypeScript.
MIT License
5.99k stars 1.14k forks source link

Recommendations and advice for proxying #1789

Open imansyed0 opened 4 months ago

imansyed0 commented 4 months ago

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;
  }
}
wiz64 commented 3 months ago

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

wiz64 commented 3 months ago

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

wiz64 commented 3 months ago

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

sunmasters commented 3 months ago

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.

wiz64 commented 3 months ago

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 !?

martincanavate commented 1 week ago

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

martincanavate commented 1 week ago

did anyone manage to solve it?

sunmasters commented 1 week ago

Yes, can be solved