instagram-private-api
instagram-private-api copied to clipboard
400 Bad Request; challenge_required
Hey,
I have a function to post image on instagram with text.
I recently changed my instagram username and I've had ‘400 Bad Request; challenge_required’ ever since.
Yet all my connection info is good, I checked.
I also confirmed that I was the one who logged into Instagram when he asked if it was me, given a new connection.
My code
const IG_USERNAME = "****";
const IG_PASSWORD = "****";
[...]
const postToInsta = async (randomImageDoc, description) => {
return retry(async () => {
const ig = new IgApiClient();
ig.state.generateDevice(IG_USERNAME);
console.log('Attempting to login to Instagram with username:', IG_USERNAME);
await ig.account.login(IG_USERNAME, IG_PASSWORD);
console.log('Logged in to Instagram successfully');
const { imageUrl, username, objects } = randomImageDoc;
console.log('Fetching image from URL:', imageUrl);
const imageBuffer = await axios.get(imageUrl, { responseType: 'arraybuffer' }).then(response => response.data);
console.log('Image fetched successfully');
const caption = `${description}\n\nCosmio's Image of the Day: shot by ${username}\n\nExplore space on Cosmio.io\n\n#astrophotography #astrophoto #astronomy #astro #cosmio #imageoftheday #space #nightsky #telescope #universe #stargaze #nightphotography #cosmos`;
console.log('Caption:', caption);
console.log('Attempting to publish photo to Instagram');
await ig.publish.photo({ file: imageBuffer, caption: caption });
console.log('Photo published successfully');
console.log('Marking the image as posted in Firestore');
await admin.firestore().collection('images').doc(randomImageDoc.id).update({ posted: true });
console.log('Image marked as posted in Firestore');
});
};
Error
In my console Firebase Functions
DEFAULT 2024-10-01T12:06:35.264569Z at Request.send (/workspace/node_modules/instagram-private-api/dist/core/request.js:54:28)
DEFAULT 2024-10-01T12:06:35.264564Z at Request.handleResponseError (/workspace/node_modules/instagram-private-api/dist/core/request.js:108:24)
DEFAULT 2024-10-01T12:06:35.264544Z Attempt 2 failed: IgCheckpointError: POST /api/v1/accounts/login/ - 400 Bad Request; challenge_required
There are many challenge types
try by setting the 2fa, using an authenticator app, not sms. and do something similar to this
await this.ig.simulate.preLoginFlow();
let loggedInUser;
try {
loggedInUser = await this.ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
} catch (err) {
if( err instanceof IgLoginTwoFactorRequiredError ){
const {username, totp_two_factor_on, two_factor_identifier} = err.response.body.two_factor_info;
const verificationMethod = totp_two_factor_on ? '0' : '1'; // default to 1 for SMS
const { code } = await inquirer.prompt([
{
type: 'input',
name: 'code',
message: `Enter code received via ${verificationMethod === '1' ? 'SMS' : 'TOTP'}`,
},
]);
// Use the code to finish the login process
loggedInUser = await this.ig.account.twoFactorLogin({
username,
verificationCode: code,
twoFactorIdentifier: two_factor_identifier,
verificationMethod, // '1' = SMS (default), '0' = TOTP (google auth for example)
trustThisDevice: '1', // Can be omitted as '1' is used by default
});
}
}
process.nextTick(async () => await this.ig.simulate.postLoginFlow());