threadsjs / threads.js

A Node.js library for the Threads API
MIT License
279 stars 29 forks source link

getToken issue #36

Closed serhaterfidan closed 1 year ago

serhaterfidan commented 1 year ago

token is getting invaild here how can I pass this part

const token = await getToken(username, password);

instagram logged successful but token is getting null here

sooluh commented 1 year ago

is it true that the token variable is null? i used three accounts with 2FA and two accounts without 2FA, and it all worked :)

serhaterfidan commented 1 year ago

instagram logged in succesful but token is getting null I am printing token on console

serhaterfidan commented 1 year ago

can you give me your email for discussions I have questions

serhaterfidan commented 1 year ago

TypeError: fetch is not a function at getToken (/Users/serhaterfidan/Desktop/threadsapi/node_modules/@threadsjs/threads.js/src/index.js:54:25) at /Users/serhaterfidan/Desktop/threadsapi/server.js:20:25 at Layer.handle [as handle_request] (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/layer.js:95:5) at next (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/route.js:144:13) at Route.dispatch (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/route.js:114:3) at Layer.handle [as handle_request] (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/layer.js:95:5) at /Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:284:15 at param (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:365:14) at param (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:376:14) at Function.process_params (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:421:3)

sooluh commented 1 year ago

try to delete node_modules and reinstall using your package manager

serhaterfidan commented 1 year ago

I tried many times remove or install again but still same error

serhaterfidan commented 1 year ago

I am using "@threadsjs/threads.js": "^1.5.4"

elijah-wright commented 1 year ago

what is your node version? undici requires 14 and above, so if you're on a lower version, update node

serhaterfidan commented 1 year ago

v14.21.3

serhaterfidan commented 1 year ago

I update node.js version to v18.16.1 and error changed like this

TypeError: getToken is not a function at /Users/serhaterfidan/Desktop/threadsapi/server.js:15:25 at Layer.handle [as handle_request] (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/layer.js:95:5) at next (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/route.js:144:13) at Route.dispatch (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/route.js:114:3) at Layer.handle [as handle_request] (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/layer.js:95:5) at /Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:284:15 at param (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:365:14) at param (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:376:14) at Function.process_params (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:421:3) at next (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:280:10)

elijah-wright commented 1 year ago

yeah, getToken got removed in 1.6.1 which got pushed this morning. try using something like this:

(async () => {
    const client = new Client();
    await client.login('username', 'password');
})();
serhaterfidan commented 1 year ago

I update the library and all code updated error is

TypeError: Cannot destructure property 'token' of 'undefined' as it is undefined. at new Client (/Users/serhaterfidan/Desktop/threadsapi/node_modules/@threadsjs/threads.js/src/index.js:12:16) at /Users/serhaterfidan/Desktop/threadsapi/server.js:15:20 at Layer.handle [as handle_request] (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/layer.js:95:5) at next (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/route.js:144:13) at Route.dispatch (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/route.js:114:3) at Layer.handle [as handle_request] (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/layer.js:95:5) at /Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:284:15 at param (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:365:14) at param (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:376:14) at Function.process_params (/Users/serhaterfidan/Desktop/threadsapi/node_modules/express/lib/router/index.js:421:3)

elijah-wright commented 1 year ago

I'll take a look at it

elijah-wright commented 1 year ago

ah, ok, you need to use new Client({});, not new Client();. i'll fix that in the readme, thanks

serhaterfidan commented 1 year ago

I updated and then error

TypeError: Cannot read properties of undefined (reading 'logged_in_user') at Client.login (/Users/serhaterfidan/Desktop/threadsapi/node_modules/@threadsjs/threads.js/src/index.js:77:28) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

elijah-wright commented 1 year ago

@sooluh you know more about this bloks stuff more than me lol. if parseBloksResponse is wrong, could mean that the username/password is wrong.

serhaterfidan commented 1 year ago

username and password is true I checked on instagram

elijah-wright commented 1 year ago

try updating to 1.6.2 and see what happens

serhaterfidan commented 1 year ago

I updated and same error do you have any test accounts for try

sooluh commented 1 year ago

I'll try again, soon.

elijah-wright commented 1 year ago

if you can, edit the file at /Users/serhaterfidan/Desktop/threadsapi/node_modules/@threadsjs/threads.js/src/index.js and put "console.log(bloks.login_response)" above it. is there anything?

sooluh commented 1 year ago

I instead encountered an error like this with the latest version:

        constructor({ token, userAgent, appId }) {
                      ^

TypeError: Cannot destructure property 'token' of 'undefined' as it is undefined.
elijah-wright commented 1 year ago

you have to pass an empty object into the client constructor.

sooluh commented 1 year ago

Sure, I found it and I will fix it now, along with the error I just encountered.

serhaterfidan commented 1 year ago

if you can, edit the file at /Users/serhaterfidan/Desktop/threadsapi/node_modules/@threadsjs/threads.js/src/index.js and put "console.log(bloks.login_response)" above it. is there anything?

console logged undefined

elijah-wright commented 1 year ago

try logging bloks, it's probably giving you an error

serhaterfidan commented 1 year ago

console logged [object Object]

sooluh commented 1 year ago

It's just a placement error. Those who use 2FA cannot directly retrieve logged_in_user.pk. This only works for accounts without 2FA.

serhaterfidan commented 1 year ago

what should I do about this

elijah-wright commented 1 year ago

should be fixed in 1.6.3

serhaterfidan commented 1 year ago

TypeError: client.getToken is not a function at /Users/serhaterfidan/Desktop/threadsapi/server.js:18:26 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

serhaterfidan commented 1 year ago

this my code

try { const { username, password } = req.query; const userId = req.params.userId;

const client = new Client({});
await client.login(username, password);

const token = client.getToken(); // Retrieve the token from the Client instance
if (!token) {
  return res.status(401).json({ error: 'Invalid credentials' });
}

const user = await client.users.fetch(userId);
res.json(user);

} catch (error) { console.error(error); res.status(500).json({ error: 'An error occurred during computation' }); }

elijah-wright commented 1 year ago

you need to use client.token if you want to get the token from the client

serhaterfidan commented 1 year ago

Token is :undefined

const client = new Client({}); await client.login(username, password);

const token = client.token; // Retrieve the token from the Client instance

console.log(`Token is :${token}`);
if (!token) {
  return res.status(401).json({ error: 'Invalid credentials' });
}
sooluh commented 1 year ago

@elijah-wright, I have just submitted a pull request. I have fixed it so that even when the Client class is initialized without an empty object, it can still retrieve the userId correctly for both accounts with and without 2FA enabled.

stevenlafl commented 1 year ago

@elijah-wright Your types/index.d.ts is wrong, you need this:

  public login(username: string, password: string): Promise<void>; 

Afterwards in typescript I can use:

    let client = new Client({});
    await client.login(username, password);

Can you update that? (btw your 1.6.3 does otherwise work and I get a token)

serhaterfidan commented 1 year ago

still same Token is :undefined

let client = new Client({}); await client.login(username, password);

const token = client.token; // Retrieve the token from the Client instance

console.log(`Token is :${token}`);
if (!token) {
  return res.status(401).json({ error: 'Invalid credentials' });
}
sooluh commented 1 year ago

@serhaterfidan Please try to update to the latest version available.

serhaterfidan commented 1 year ago

I am using 1.6.4

elijah-wright commented 1 year ago

could you console.log(client)? you might need to wrap everything in an async function

serhaterfidan commented 1 year ago

Client { _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined, token: null, userAgent: 'Barcelona 289.0.0.77.109 Android', appId: '238260118697367', androidId: '3hp0if058dk00000', userId: null, rest: RESTManager {}, users: UserManager {}, posts: PostManager {}, feeds: FeedManager {},

} TypeError: Cannot read properties of undefined (reading 'logged_in_user') at Client.login (/Users/serhaterfidan/Desktop/threadsapi/node_modules/@threadsjs/threads.js/src/index.js:135:28) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async /Users/serhaterfidan/Desktop/threadsapi/server.js:17:5

sooluh commented 1 year ago

What you are currently testing, is it using 2FA or not?

elijah-wright commented 1 year ago

try 1.6.5

serhaterfidan commented 1 year ago

I don't have 2FA on my account

sooluh commented 1 year ago

try 1.6.5

I think that code will remove the userId, does it mean not everyone has a userId? This is very strange.

serhaterfidan commented 1 year ago

I updated but error is same

Client { _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined, token: null, userAgent: 'Barcelona 289.0.0.77.109 Android', appId: '238260118697367', androidId: 'q3icvec75r40000', userId: null, rest: RESTManager {}, users: UserManager {}, posts: PostManager {}, feeds: FeedManager {},

} TypeError: Cannot read properties of undefined (reading 'logged_in_user') at Client.login (/Users/serhaterfidan/Desktop/threadsapi/node_modules/@threadsjs/threads.js/src/index.js:135:28) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async /Users/serhaterfidan/Desktop/threadsapi/server.js:17:5

serhaterfidan commented 1 year ago

do you have any test account for test

sooluh commented 1 year ago

What happens if you add console.log(JSON.stringify(bloks)); on line 134 of /Users/serhaterfidan/Desktop/threadsapi/node_modules/@threadsjs/threads.js/src/index.js?

serhaterfidan commented 1 year ago

{"error":{"uid":{},"code":1,"message":"Unmapped IG Error: This IG Error was not mapped to an Error Code. To fix it, update the error tool under 'CAA' to map it to an Error Code.","error_user_title":"","error_subcode":3540001,"error_user_msg":"challenge_required","error_data":{"url":"https://i.instagram.com/challenge/AXFFZ_wIFA7sub4MHuaGzs3HUJDU22k_BgCDnz406sREk3A943NZIBhNb2XUB8Fvc224aTc/AfzSC2aoP5qwsZrIiT6RmizVKvHX-sKyL7ZVdgmbLUrkTWY35NGR7f5Qxu67ejUuFQwPKgys7U-sLA/","api_path":"/challenge/AXFFZ_wIFA7sub4MHuaGzs3HUJDU22k_BgCDnz406sREk3A943NZIBhNb2XUB8Fvc224aTc/AfzSC2aoP5qwsZrIiT6RmizVKvHX-sKyL7ZVdgmbLUrkTWY35NGR7f5Qxu67ejUuFQwPKgys7U-sLA/","hide_webview_header":true,"lock":true,"logout":false,"native_flow":true,"flow_render_type":0,"challenge_context":{"step_name":"","nonce_code":"AfzkvmeG5NK15lobGnJYLjKWoBAO6sbp4mTsIBDm9OtXzvoVkmd4di6gpxMVUYa2mad25XSA978GCw","user_id":"AXHNfftbsNK4yA8Jx7xXplcunOCHbpkSY2P2pcjbn0rJYe3KJv4A4gJ8V0DGkUTMZ8lloNA","first_factor_code":"YUGA9l13dydJ0vE7NLqYutx9fvQzMQnyBCtcegDHWK7ysGabyHXjvcdSG3J62E6o","is_stateless":false,"present_as_modal":false}}}}

sooluh commented 1 year ago

Damn, your account has been hit with a checkpoint. Please check your Instagram application, you may be required to change your password.