instaloader: Login error

Hi,

I got error KeyError: "name='csrftoken', domain=None, path=None", when trying to login

instaloder ver : 4.9.5

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 28
  • Comments: 45 (1 by maintainers)

Most upvoted comments

Thanks @IL2C! I have tested your code and can confirm logging in does not produce an error anymore. So for those of you who cannot wait to use Instaloader again:

pip uninstall instaloader
pip install git+https://github.com/IL2C/instaloader.git@login-fix

(Of course you should always double-check when you’re installing code from someone else’s repository. But at the time of posting the repo was exactly 1 commit ahead of the Instaloader main repo, with only the headers changed.)

I think I have a workaround:

* Use Firefox to login to Instagram

* Use the script from [here](https://instaloader.github.io/troubleshooting.html#login-error) to copy the session file from Firefox to the Instaloader path.

* Use the following code to log in:
USERNAME = "your_username"

import instaloader

L = instaloader.Instaloader()
L.load_session_from_file(USERNAME)

This is how I fixed it:

HOW TO FIX KeyError: "name='csrftoken', domain=None, path=None" BUG (assuming you have Instaloader already installed)

STEP 1:

Follow these steps: (based on: https://instaloader.github.io/troubleshooting.html#login-error )

A. Download the script: Example 615_import_firefox_session.py https://raw.githubusercontent.com/instaloader/instaloader/master/docs/codesnippets/615_import_firefox_session.py B. Open Firefox and go to the “Privacy & Security” settings (about:preferences#privacy) C. Change “History”-Settings from “Use custom settings for history” -> “Always use custom browsing mode” to “Remember history”.

“Firefox must restart to disable this feature.” -> “Restart Firefox now” D. Open Firefox again and login to Instagram in Firefox with whatever account you then wish to use for Instaloaders.

STEP 2:

A. Open CMD as administrator and go to wherever you downloaded the script to B. Execute python 615_import_firefox_session.py

Your Instagram-session should be imported now and usable!

STEP 3:

Problem solved, now execute any Instaloader command using the “–login=usernameFromSession” argument

@evanmiltenburg thanks, it works!

Fixed in v4.10.1 thanks to #2088.

Replacing the default user agent with any from this list resolves this issue. https://github.com/instaloader/instaloader/blob/ffb0aea5eb79b58ebad1eee57ad097bc2aba0689/instaloader/instaloadercontext.py#L34

From what I’ve gathered, using a user agent that doesn’t correlate Instagram software won’t access the csrftoken cookie when sending the post request to login. https://github.com/instaloader/instaloader/blob/3cc29a4ceb3ff4cd04a73dd4b20979b03128f454/instaloader/instaloadercontext.py#L222

Upading the user agent worked for me.

Quick guide on how to do that without relying on an external fork:

  1. clone the repository
  2. open the file instaloader/instaloader/instaloadercontext.py in a text editor
  3. locale the function default_user_agent, currently it’s on line 34
  4. edit the user agent string to one of these https://developers.whatismybrowser.com/useragents/explore/software_name/instagram/ you can add hashtags in front of the existing user agent make it a comment for future reference and add the new user agent below it, make sure to indent using spaces, write return 'insert the user agent you chose'
  5. navigate to the top level directory using your terminal
  6. run pip install ./instaloader, your existing installation should get uninstalled, your local edited one should get installed
  7. if you want to return to the upstream version run pip install instaloader

I am preparing a file, to upload (which will be an explanation of the problems we are having, and why. But it is an unfinished version. Although enough to explain, certain things. The reason for not finishing it quickly, is that my English It is very bad, and use the Google Translate a lot, which is not very good either. And as I am Spanish, well …

Greetings.

I think I have a workaround:

  • Use Firefox to login to Instagram
  • Use the script from here to copy the session file from Firefox to the Instaloader path.
  • Use the following code to log in:
USERNAME = "your_username"

import instaloader

L = instaloader.Instaloader()
L.load_session_from_file(USERNAME)

Now Instagram kicks me out even though I use @evanmiltenburg’s work-around. Anyone else?

The only thing the code does is changes the default user agent. You can override the user agent more easily with a command line parameter. In any case, I still get instant ban when I try to login using this code and/or the specified user agent.

Hello, Working for me with parameter : --user-agent And user agent in this list : https://developers.whatismybrowser.com/useragents/explore/software_name/instagram/ Mathieu

Hello,

I have the same problem with 4.9.5 (windows standalone).

Do you have a solution for this binary version ?

Thanks, Mathieu

Thanks @IL2C! I have tested your code and can confirm logging in does not produce an error anymore. So for those of you who cannot wait to use Instaloader again:

pip uninstall instaloader
pip install git+https://github.com/IL2C/instaloader.git@login-fix

(Of course you should always double-check when you’re installing code from someone else’s repository. But at the time of posting the repo was exactly 1 commit ahead of the Instaloader main repo, with only the headers changed.)

Works for me with no issues. Thanks!

Many thanks for workarounds or corrections, I’ll try them and keep in touch ! Thanks for all this work !

Vincent,

Envoyé de mon SmartPhone .

On 8 Oct 2022, at 19:05, Emiel van Miltenburg @.***> wrote:

Of you have a working version, can you perhaps submit a pull request?

On 8 Oct 2022, at 18:34, ILikeToCode @.***> wrote:

Replacing the default user agent with any from this list resolves this issue. https://github.com/instaloader/instaloader/blob/ffb0aea5eb79b58ebad1eee57ad097bc2aba0689/instaloader/instaloadercontext.py#L34

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

Not working for me on MacOS. I can see the “csrftoken” in the sql file, but after running the script, the error shows up again.

I’ve also tried to do the login directly after the “instaloader.save_session_to_file(sessionfile)” in the script -> no difference, same error.

I think I have a workaround:

  • Use Firefox to login to Instagram
  • Use the script from here to copy the session file from Firefox to the Instaloader path.
  • Use the following code to log in:
USERNAME = "your_username"

import instaloader

L = instaloader.Instaloader()
L.load_session_from_file(USERNAME)

Downloading the session file is no problem but this still doesn’t work :

* logging in this way seems to fail but doesn't throw any error. It's until you execute the command "instaloader -l USERNAME" that then the password is required (new ?), and then get the original error above

* trying to log in without session file will throw you the error right away

Trying to follow this method I even get the following error:

Using cookies from C:\Users\MYUSERNAME/AppData/Roaming/Mozilla/Firefox/Profiles\SESSIONFILENAME.default-release\cookies.sqlite.
Not logged in. Are you logged in successfully in Firefox?

I am currently logged into Instagram. Not sure what else to do? Wasn’t there a browser plugin that could download Sessions/Cookies?

Edit: Only works in non-private mode. Had it enabled by default.

Edit 2: After importing the session it begins working.