jlobos / instagram-web-api

🤳 Instagram Private Web API client for Node
https://npmjs.com/instagram-web-api
MIT License
1.12k stars 185 forks source link

Cannot read properties of null (reading '0') #302

Open mohamedsabith opened 1 year ago

mohamedsabith commented 1 year ago

const matches = res.toJSON().body.match(pattern) value = matches[0].substring(13) TypeError: Cannot read properties of null (reading '0') node_modules\instagram-web-api\lib\index.js:57:22

Strooss commented 1 year ago

having the same issue here..

ColdFire87 commented 1 year ago

I think the issue is with this code new RegExp(/(csrf_token":")\w+/). It expects the quotes to not be escaped.

Doing a console.log(res.toJSON().body) ... I can see 2 matches for csrf_token ... the first one contains the actual value, but has the quotes escaped -> csrf_token\":\"...\" ... the second one is not escaped, but the value is empty -> csrf_token":"".

The way I got it to work was to replace escaped quotes with non-escaped ones, before matching the regex:

await this.request('/', { resolveWithFullResponse: true }).then(res => {
      const pattern = new RegExp(/(csrf_token":")\w+/)
      const matches = res.toJSON().body.replace(/\\"/g, '"').match(pattern)
      value = matches[0].substring(13)
    })
Strooss commented 1 year ago

I think the issue is with this code new RegExp(/(csrf_token":")\w+/). It expects the quotes to not be escaped.

Doing a console.log(res.toJSON().body) ... I can see 2 matches for csrf_token ... the first one contains the actual value, but has the quotes escaped -> csrf_token\":\"...\" ... the second one is not escaped, but the value is empty -> csrf_token":"".

The way I got it to work was to replace escaped quotes with non-escaped ones, before matching the regex:

await this.request('/', { resolveWithFullResponse: true }).then(res => {
      const pattern = new RegExp(/(csrf_token":")\w+/)
      const matches = res.toJSON().body.replace(/\\"/g, '"').match(pattern)
      value = matches[0].substring(13)
    })

i think the package is completely broken..

dimaspriyanto commented 1 year ago

For my case, Instagram identify unusual login, when i follow the checkpoint URL, they required me to change my password before continue..

StatusCodeError: 400 - {"message":"checkpoint_required","checkpoint_url":"/challenge/action/AXEKy63B9j8ILsr8huijpQn9PEYOzBiY-5vpbN6PDFgowATNdyZrUX1VXivUQaLV-JTF/Afz24OgDEt9wmXbBpuUFsCytJ_mtvkP7OT3aYhJEtJZtsy0dtVT1_qlvE426vXD6vBgLMIO8u-OUVw/ffc_UEvY9OEISw45TL7Pr9Uu6ASkxGOMKWYGuv7xl3Jjodk40sV7IzWyJvaXH63VVOo1/","lock":false,"flow_render_type":0,"status":"fail"}

AliAryanTech commented 1 year ago
TypeError: Cannot read properties of null (reading '0')

++

technical-shoubhik commented 1 year ago

(node:2668) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of null at D:\Codes\NodeJS\ins\node_modules\instagram-web-api\lib\index.js:57:22 at processTicksAndRejections (internal/process/task_queues.js:95:5) at async Instagram.login (D:\Codes\NodeJS\ins\node_modules\instagram-web-api\lib\index.js:54:5) (Use node --trace-warnings ... to show where the warning was created) (node:2668) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:2668) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

quaint-racoon commented 1 year ago

Dude 2 months still not solved

erhancan256 commented 1 year ago

Dude 2 months still not solved

i have same problem pls help

quaint-racoon commented 1 year ago

I found a solution but theres another porblem

Edit the package and replace the errored code with this

let value = this.request('/', { resolveWithFullResponse: true }).then(res => { const pattern = new RegExp(/(csrf_token\":\")[\w]+/) const matches = res.body.match(pattern) value = matches[0].substring(15) })

But another error apears

dmzoneill commented 1 year ago

just tried to use the moduile and hit this also

Apr 04 19:17:51 dave-pc node[1278826]:       value = matches[0].substring(13)
Apr 04 19:17:51 dave-pc node[1278826]:                      ^
Apr 04 19:17:51 dave-pc node[1278826]: TypeError: Cannot read properties of null (reading '0')
Apr 04 19:17:51 dave-pc node[1278826]:     at /home/dave/src/whatsbot/node_modules/instagram-web-api/lib/index.js:57:22
Apr 04 19:17:51 dave-pc node[1278826]:     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Apr 04 19:17:51 dave-pc node[1278826]:     at async Instagram.login (/home/dave/src/whatsbot/node_modules/instagram-web-api/lib/index.js:54:5)

disappointing

SGauts commented 1 year ago

Facing the same issue. value = matches[0].substring(13) ^ TypeError: Cannot read properties of null (reading '0') Any fix ?

alexandre-hallaine commented 1 year ago

i made a fix, just replace this

    let value
    await this.request('/', { resolveWithFullResponse: true }).then(res => {
      const pattern = new RegExp(/(csrf_token":")\w+/)
      const matches = res.toJSON().body.match(pattern)
      value = matches[0].substring(13)
    })

with this

    let value
    await this.request('/', { resolveWithFullResponse: true }).then(res => {
      const pattern = new RegExp(/(csrf_token\\":\\")\w+/)
      const matches = res.toJSON().body.match(pattern)
      if (!matches || matches.length === 0)
        throw new Error('Missing CSRFToken')
      value = matches[0].substring(15)
    })

in the file lib/index.js line 53

quaint-racoon commented 1 year ago

i made a fix, just replace this

    let value
    await this.request('/', { resolveWithFullResponse: true }).then(res => {
      const pattern = new RegExp(/(csrf_token":")\w+/)
      const matches = res.toJSON().body.match(pattern)
      value = matches[0].substring(13)
    })

with this

    let value
    await this.request('/', { resolveWithFullResponse: true }).then(res => {
      const pattern = new RegExp(/(csrf_token\\":\\")\w+/)
      const matches = res.toJSON().body.match(pattern)
      if (!matches || matches.length === 0)
        throw new Error('Missing CSRFToken')
      value = matches[0].substring(15)
    })

in the file lib/index.js line 53

but this creates a new error if u tried running it a request error

alexandre-hallaine commented 1 year ago

but this creates a new error if u tried running it a request error

can you give the error you got?

larkrak commented 1 year ago

but this creates a new error if u tried running it a request error

can you give the error you got?

I think the previous guy is talking about this error:

photo_2023-05-28_21-10-49

Which is happening to me after your fix. I inspected all the output from this function: 343a76dd80e8eb90b9a8aa876810b853 Saving it in a file and looking for "window._sharedData" which is the split condition, and there is no "window._sharedData" on that html string.

Is this some change from instagram? Do we know a fix for this?