I can't log in to Instagram: "CSRF token missing or incorrect"
Asked Answered
R

5

24

I was using Selenium Python to log in to Instagram and open some pages. It worked fine, but after two days the Instagram started sending the message "CSRF token missing or incorrect". And now I can't even log in with my script or manually to any accounts and with any browsers such as Chrome or FireFox on my laptop.

I'm not sending any cookies with my Selenium. And most of the search results are about Django which I'm not using.

  • I erased the cookies, but it it didn't work.

  • I tried to change my IP address to make sure if I'm banned from Instagram, but it didn't work.

  • I tried to check for the scrf-token in my URL with Selenium and sending it to the driver, but it didn't work.

I'm not sure if the solution is within the code, because now I can't log in even manually, so maybe there must be a problem with my system settings or from Instagram side.

Can I fix this with Selenium? Or how can I fix this?

Rusty answered 29/10, 2022 at 8:31 Comment(2)
A better site gor this question is webmastersBeamer
For me, clearing all cookies and data in my browser for instagram.com fixed the problemThundersquall
S
32

One liner to run in JS console in your browser to fix CSRF token issue:

n=new Date;t=n.getTime();et=t+36E9;n.setTime(et);document.cookie='csrftoken='+document.body.innerHTML.split('csrf_token')[1].split('\\"')[2]+';path=\;domain=.instagram.com;expires='+n.toUTCString();

Open JS console, copy paste it, press enter. Reload the page and try to login again. This js snippet will find CSRF token in page source and create cookie for you.

Spinoza answered 30/11, 2022 at 16:58 Comment(0)
W
18

Open your Chrome browser developer tools, and then go to the login page of Instagram. In the network tab, find the request that goes like this: https://www.instagram.com/

Click on the request and in the response tab, press Ctrl + F and search for csrf_token. Copy the value of csrf, and go to the application tab. In the storage section, click on cookies and insert a cookie with the csrftoken name. Paste the value and make it secure. Now fill the login form and press Enter.

Wisconsin answered 9/11, 2022 at 8:54 Comment(6)
Just note that after entering the csrftoken cookie and entering the username and password, Instagram will ask you for your username and password "again", which will log in to the account after re-entering the username and password.Annates
But, how can I do it on the mobile browser??Dinny
@Dinny connect your mobile to a desktop with chrome browser, go to chrome://inspect#device url and inspect instagram url then do the same.Wisconsin
Thankful. I was able to login. But before that, I had to create a new cookie in the console tab. Because in the application tab, it is only possible to edit the cookie and you cannot add a new cookie.Dinny
@Wisconsin Unfortunately, devtools can only be used for chrome. But my problem is with the software that uses its own internal browser and cannot log in.Dinny
This is literally the answer. You might wanna mark this one as answer, so people can reach out easier. Also for those who might struggle on finding a JSON parameter's value, you need to grab the value between double quotes (") like the example: \"csrf_token\":\"WJotfAwa012mnuleedpfCiZoLrN\" and the csrftoken value would be: WJotfAwa012mnuleedpfCiZoLrNCockaigne
S
8

I had the same problem. I used a VPN to connect to the US, and there wasn't any "CSRF token missing" message after that.

Sexagenarian answered 9/11, 2022 at 8:46 Comment(0)
M
1

As mentioned @350D this way probably work, but it doesn't work for me. I had to remove my local storage (from inspect). and if you have two step verification, you remove it temporarily.

Monti answered 15/2, 2023 at 13:26 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewPenelope
P
-1

Firefox

Open the Firefox Options menu.
On the left, select Privacy & Security.
Under Cookies and Site Data click on Manage Exceptions.
    Copy and paste "https://example.com" and click Allow.

Click Save Changes.
Next, click on Manage Data.
Search for todoist and select Remove All Shown.
Click Save Changes and confirm in the pop-up window by clicking Remove.
Reload Firefox and log into Todoist.

Chrome

Open Chrome Settings.
In the Privacy and security section, click Cookies and other site data.
Scroll down to Sites that can always use cookies and click Add.
    Copy and paste "[*.]example.com" and click Add.
Click See all cookies and site data, search for todoist, and delete all Todoist-related entries.
Reload Chrome and log into Todoist.
Prado answered 18/1, 2023 at 17:13 Comment(1)
What is "Todoist"? And what has it to do with logging in into Instagram with Selenum?Conclusion

© 2022 - 2024 — McMap. All rights reserved.