amazon-kindle-bulk-downloader icon indicating copy to clipboard operation
amazon-kindle-bulk-downloader copied to clipboard

Failed to parse CRSF token

Open psotos opened this issue 9 months ago • 15 comments

I am getting the following error:

$ bun run src/index.ts
Getting credentials
Got credentials
Filling username
Filling password
69 |   const cookie = (await page.browser().cookies())
70 |     .map((c) => c.name + "=" + c.value)
71 |     .join(";");
72 |   // @ts-expect-error - This is set by the page
73 |   const csrfToken = await page.evaluate(() => window.csrfToken);
74 |   if (!csrfToken) throw new Error("Failed to get csrfToken");
                             ^
error: Failed to get csrfToken
      at <anonymous> (/Users/<mymacusername>/amazon-kindle-bulk-downloader/src/index.ts:74:25)

Bun v1.2.2 (macOS arm64)
error: script "start" exited with code 1

psotos avatar Feb 19 '25 01:02 psotos

I got this until I deleted my Passkey. It'll prompt you to make a new one the next time you log in.

maxpinton avatar Feb 19 '25 04:02 maxpinton

How do I delete that passkey?

psotos avatar Feb 19 '25 04:02 psotos

Your Account > Login & Security > Passkey > (garbage can icon)

maxpinton avatar Feb 19 '25 04:02 maxpinton

I've tried that a bunch of times both in Amazon and in my Google Account and no dice. I am still getting the CSRFToken error. :(

Getting credentials
Got credentials
Filling username
Filling password
69 |   const cookie = (await page.browser().cookies())
70 |     .map((c) => c.name + "=" + c.value)
71 |     .join(";");
72 |   // @ts-expect-error - This is set by the page
73 |   const csrfToken = await page.evaluate(() => window.csrfToken);
74 |   if (!csrfToken) throw new Error("Failed to get csrfToken");
                             ^
error: Failed to get csrfToken

psotos avatar Feb 19 '25 04:02 psotos

I got passed the passkey issue but sadly it says that I have 0 books total out of the actual thousands that I have. I was able to download a book by hand but dont want to go through the exercise of downloading all of my books manually.

$ bun run src/index.ts
Getting credentials
Got credentials
Filling username
Filling password
Got auth
Got device <my crazy device name here>
Found 0 books in total

psotos avatar Feb 19 '25 04:02 psotos

Did you try bun run start --baseUrl https://www.amazon.com/ ? The baseUrl is necessary.

maxpinton avatar Feb 19 '25 04:02 maxpinton

Did you try bun run start --baseUrl https://www.amazon.com/ ? The baseUrl is necessary.

The base url has a default that can be changed. Probably not related.

tcurdt avatar Feb 19 '25 12:02 tcurdt

Try bun run start --baseUrl https://www.amazon.com/ --manualAuth true ... that did it for me.

laststrawman avatar Feb 19 '25 16:02 laststrawman

@laststrawman this did it for me, too. Note: @psotos specifically I ran bun run start --manualAuth --baseUrl "https://www.amazon.com"

inlikealion avatar Feb 19 '25 16:02 inlikealion

Actually exporting the credentials as environment variables (in git bash, 'export OTP=123456') instead of storing them in an .env file did the trick for me.

Nightsky770 avatar Feb 20 '25 05:02 Nightsky770

Did you try bun run start --baseUrl https://www.amazon.com/ ? The baseUrl is necessary.

This worked for me. I use amazon.com so I used the url exactly as you had it. I assumed that was the default and I wouldn't need to specify.

If the process doesn't work without specifying, maybe an error should display if it's omitted.

Or if it only works sometimes, perhaps if 0 books are found a note should be displayed underneath: If you believe this result is an error, please make sure you have specified the baseUrl parameter in your command as this is sometimes necessary to retrieve your books.

dipique avatar Feb 20 '25 18:02 dipique

I also got this CSRF error (amazon.com, macOS). I deleted my passkey and that did not fix the issue. However, adding the --manualAuth flag allowed me to sign in successfully and then the script worked as expected.

jeremiahlee avatar Feb 20 '25 21:02 jeremiahlee

In general these CSRF related issues point to the fact that your login did not 100% succeed (for whatever reason). Whilst I am working to improve this — for now, --manualAuth will be your best bet given the tight deadline for amazon stopping support for this feature.

treetrum avatar Feb 21 '25 07:02 treetrum

I encountered this because I have not given Amazon my telephone number. Manual login worked great. Thank you!

EricWVGG avatar Feb 21 '25 17:02 EricWVGG

Having issues with --manualAuth. Puppeteer opens with a captcha to confirm that it isn't bot traffic (they may be on to you) then the following sign in page continuously loads and never progresses once the sign in button is pressed.

ltcworf avatar Feb 26 '25 02:02 ltcworf