Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix login method #38

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { parseBloksResponse } = require("./util/Bloks.js");
const androidId = (Math.random() * 1e24).toString(36);

class Client extends EventEmitter {
constructor({ token, userAgent, appId }) {
constructor({ token, userAgent, appId ) {
super({ captureRejections: true });

this.token = token || null;
Expand All @@ -30,7 +30,7 @@ class Client extends EventEmitter {
async login(username, password) {
const base = "https://i.instagram.com";
const loginUrl = "/api/v1/bloks/apps/com.bloks.www.bloks.caa.login.async.send_login_request/";

const params = {
client_input_params: {
password: password,
Expand All @@ -42,20 +42,20 @@ class Client extends EventEmitter {
device_id: `android-${androidId}`,
},
};

const bkClientContext = {
bloks_version:
"5f56efad68e1edec7801f630b5c122704ec5378adbee6609a448f105f34a9c73",
styles_id: "instagram",
};

const requestBody = {
params: JSON.stringify(params),
bk_client_context: JSON.stringify(bkClientContext),
bloks_versioning_id:
"5f56efad68e1edec7801f630b5c122704ec5378adbee6609a448f105f34a9c73",
};

const requestOptions = {
method: "POST",
headers: {
Expand All @@ -69,23 +69,24 @@ class Client extends EventEmitter {
requestBody.bk_client_context,
)}&bloks_versioning_id=${requestBody.bloks_versioning_id}`,
};

const response = await fetch(base + loginUrl, requestOptions);
const text = await response.text();
const bloks = parseBloksResponse(text);

if (bloks.two_factor_required) {
const {
two_factor_identifier,
trusted_notification_polling_nonce
} = bloks.two_factor_info

console.log('Please approve the login request on your Instagram')


const self = this;
const token = await new Promise((resolve) => {
const statusUrl = "/api/v1/two_factor/check_trusted_notification_status/";
const verifyUrl = "/api/v1/accounts/two_factor_login/";

const interval = setInterval(async function () {
requestOptions.body = new URLSearchParams({
two_factor_identifier: two_factor_identifier,
Expand All @@ -94,10 +95,10 @@ class Client extends EventEmitter {
trusted_notification_polling_nonces:
JSON.stringify([trusted_notification_polling_nonce]),
}).toString();

const response = await fetch(base + statusUrl, requestOptions);
const json = await response.json();

if (json.review_status === 1) {
requestOptions.body = new URLSearchParams({
signed_body: 'SIGNATURE.' + JSON.stringify({
Expand All @@ -110,25 +111,31 @@ class Client extends EventEmitter {
verification_method: '4'
})
}).toString()

const response = await fetch(base + verifyUrl, requestOptions);
const json = await response.json();
const header = response.headers.get('Ig-Set-Authorization');


if (json.logged_in_user.pk) {
self.userId = json.logged_in_user.pk;
}

clearInterval(interval);
const token = header.replace("Bearer IGT:2:", "") || null

resolve(token);
}
}, 2_500);
});

this.token = token;
return;
}

if (bloks.login_response && bloks.login_response.logged_in_user.pk) {
if (bloks.login_response.logged_in_user.pk) {
this.userId = bloks.login_response.logged_in_user.pk_id;
}

this.token = bloks.headers?.["IG-Set-Authorization"].replace("Bearer IGT:2:", "");
}
}
Expand Down