instaloader / instaloader

Download pictures (or videos) along with their captions and other metadata from Instagram.
https://instaloader.github.io/
MIT License
8.51k stars 1.15k forks source link

Login error: 400 Bad Request #92

Closed jonathanlaniado closed 4 years ago

jonathanlaniado commented 6 years ago

Logging in used to work, but I get this error every time now.

If I go to www.instagram.com, I get a suspicious activity/unusual login attempt page. Even after confirming that, yes, "This was me", I receive the same login connection error and another suspicious activity page.

aandergr commented 6 years ago

Unfortunately, I cannot reproduce this error, making it impossible to me for fix it. Could you provide more information about when it happens?

aandergr commented 6 years ago

Does this problem still persist? If so, does our 4.0 pre-release alleviate the problem?

pip3 install --upgrade --pre instaloader~=4.0rc1
secure12 commented 6 years ago

Yes, the problem still persists. If one calls the function instaloader.Instaloader().login/interactive_login with valid username and password, instaloader.exceptions.ConnectionException: Login error: 400 Bad Request will prompt.

secure12 commented 6 years ago

One interesting observation is that the "suspicious" login time as shown on Instagram is not the same as actual login attempt time, e.g. the actual time: 28-May-2018 3:31 AM (UTC+8), login attempt time shown on Instagram: 27-May-2018 12:31 PM (US West Coast?).

nagualcode commented 6 years ago

I face this same problem when loggin with instaloader from a vps located in UK. I have tried loggin with web browser from that same vps, it works, I face the "unusual loggin" challenge, but instaloader is never able to login from that same machine.

EDIT: I do believe the vps IP may be on a blacklist, that blocks bots. I do believe thay somewhat allow "easygoing" bots, from home ips.

cristianCarrerasCastillo commented 5 years ago

ConnectionException('Login error: 400 Bad Request',) I use Instaloader 4.1.1 and I also have that error (country chile, south america)

MattrAus commented 5 years ago

I got this error while I had 2FA enabled. I disabled it and was able to login.

Hope this helps.

PhantomTech commented 5 years ago

In cases where 2FA is disabled, this issue seems to be caused when Instagram detects the login as suspicious. When this happens Instagram asks the user to verify the login with a code sent to the user's registered phone number or email address. https://github.com/rarcega/instagram-scraper seems to handle this through its interactive login (-i) feature, maybe something similar could be implemented here?

aandergr commented 5 years ago

With version 4.2rc2 we changed how Instaloader's login function copes with failure responses (commit 4ee867c61b41db14cef7bfb7897519046540a242 and 89b56bdfa8af0c45d4c18cf60bbca12d7fb1bbf5). Could the ones affected here please try that version and comment which error message is shown?

Upgrade with

pip3 install --upgrade git+https://github.com/instaloader/instaloader
Zachary24 commented 5 years ago

i'm getting Fatal error: Login error: "fail" status, message "checkpoint_required".. I don't get why Instagram does this, I have 2FA off, which means I don't want it on, right? Worst thing is that it still doesn't work after "verifying" on the app or website.

aandergr commented 5 years ago

I still cannot reproduce this - I can login perfectly. I can provoke a suspicious login checkpoint challenge by doing many fail logins, but when opening the URL provided by Instaloader I can solve the challenge and login with Instaloader without any problems.

Based on @e5150 comment in https://github.com/instaloader/instaloader/issues/217#issuecomment-449820743 I made the following snippet to workaround login problems by importing the session cookies from Firefox and bypassing Instaloader's login. (the linked comment also describes how to do that with Chromium)

from glob import glob
from os.path import expanduser
from sqlite3 import connect

from instaloader import ConnectionException, Instaloader

# FIREFOXCOOKIEFILE = "/home/alex/.mozilla/firefox/l96w6b90.default/cookies.sqlite"
FIREFOXCOOKIEFILE = glob(expanduser("~/.mozilla/firefox/*.default/cookies.sqlite"))[0]

instaloader = Instaloader(max_connection_attempts=1)
instaloader.context._session.cookies.update(connect(FIREFOXCOOKIEFILE)
                                            .execute("SELECT name, value FROM moz_cookies "
                                                     "WHERE baseDomain='instagram.com'"))

try:
    username = instaloader.test_login()
    if not username:
        raise ConnectionException()
except ConnectionException:
    raise SystemExit("Cookie import failed. Are you logged in successfully in Firefox?")

instaloader.context.username = username
instaloader.save_session_to_file()

To use this,

  1. login to Instagram in Firefox,
  2. execute the snippet, then
  3. instaloader -l USERNAME should work fine

If you do not use your default firefox profile, or your operating system has the paths differently set up than mine (Debian Buster with Firefox 60), you may have to alter the FIREFOXCOOKIEFILE variable first.

Could the ones affected by this issue please try this out?

alessandro-gangi commented 5 years ago

I run my app on and external server so i can't test the solution above.

I tried this procedure (found on the web):

It worked at the first login but it gave me the error when i tried to login for the second time. Any updates?

e5150 commented 5 years ago

I run my app on and external server so i can't test the solution above.

Well, you could generate the session file on your desktop, then copy it to the server where Instaloader is running.

alessandro-gangi commented 5 years ago

I tried and it worked! Thanks :)

amitupreti commented 5 years ago

I still cannot reproduce this - I can login perfectly. I can provoke a suspicious login checkpoint challenge by doing many fail logins, but when opening the URL provided by Instaloader I can solve the challenge and login with Instaloader without any problems.

Based on @e5150 comment in #217 (comment) I made the following snippet to workaround login problems by importing the session cookies from Firefox and bypassing Instaloader's login. (the linked comment also describes how to do that with Chromium)

from glob import glob
from os.path import expanduser
from sqlite3 import connect

from instaloader import ConnectionException, Instaloader

# FIREFOXCOOKIEFILE = "/home/alex/.mozilla/firefox/l96w6b90.default/cookies.sqlite"
FIREFOXCOOKIEFILE = glob(expanduser("~/.mozilla/firefox/*.default/cookies.sqlite"))[0]

instaloader = Instaloader(max_connection_attempts=1)
instaloader.context._session.cookies.update(connect(FIREFOXCOOKIEFILE)
                                            .execute("SELECT name, value FROM moz_cookies "
                                                     "WHERE baseDomain='instagram.com'"))

try:
    username = instaloader.test_login()
    if not username:
        raise ConnectionException()
except ConnectionException:
    raise SystemExit("Cookie import failed. Are you logged in successfully in Firefox?")

instaloader.context.username = username
instaloader.save_session_to_file()

To use this,

  1. login to Instagram in Firefox,
  2. execute the snippet, then
  3. instaloader -l USERNAME should work fine

If you do not use your default firefox profile, or your operating system has the paths differently set up than mine (Debian Buster with Firefox 60), you may have to alter the FIREFOXCOOKIEFILE variable first.

Could the ones affected by this issue please try this out?

worked perfectly. I just copied the session generated

NoobQuestions commented 5 years ago

I've got exactly the same issue, instaloader cannot login, even after verifying in the browser. But when I try to execute the snippet, it says from: can't read /var/mail/glob from: can't read /var/mail/os.path from: can't read /var/mail/sqlite3 from: can't read /var/mail/instaloader /home/xxx/Desktop/isnta.sh: line 8: syntax error near unexpected token(' /home/xxx/Desktop/isnta.sh: line 8: FIREFOXCOOKIEFILE = glob(expanduser("~/.mozilla/firefox/*.default/cookies.sqlite"))[0]' and when I try to do it manually via the python snippet in #217 (comment), it says >>> data = pickle.load(open('/home/xxx/.mozilla/firefox/xxx.default/cookies.sqlite', 'rb')) Traceback (most recent call last): File "<stdin>", line 1, in <module> _pickle.UnpicklingError: the STRING opcode argument must be quoted

What can I do?

avatarslove commented 5 years ago

TO EVERYONE CURRENTLY WITH THIS ISSUE:

I fiddled around for almost 30 mins trying to get this to work with disabling 2FA, manually submitting a cert, changing my password, etc. But what fixed it all was chaning my vpn location to about 200 miles away and instagram let me in. Hope this helps you guys

mdPlusPlus commented 5 years ago

@avatarslove : Who's your VPN provider? I'm having similar issues with Mullvad.net currently. Switching VPN exit points doesn't help, however.

laurakunst commented 5 years ago

I tried and it worked! Thanks :)

Hi @piggaruolo! I'm new to running instaloader on a server (gcloud compute engine). On what directory did you save it?

Thanks!

github-actions[bot] commented 4 years ago

There has been no activity on this issue for an extended period of time. This issue will be closed after further 14 days of inactivity.

sofie-thorsen commented 4 years ago

Could

When I run this I get an operational error saying it is unable to open database file. Any clue how I resolve this issue?

The full error message:


OperationalError Traceback (most recent call last)

in 13 14 instaloader = Instaloader(max_connection_attempts=1) ---> 15 instaloader.context._session.cookies.update(connect(FIREFOXCOOKIEFILE).execute("SELECT name, value FROM moz_cookies " 16 "WHERE baseDomain='instagram.com'")) 17 OperationalError: unable to open database file
nagualcode commented 4 years ago

maybe the system user you are running doesnt have permissions to access some folder where instaloader is trying to save this database file? Maybe your disk is full?

rocailler commented 4 years ago

I still cannot reproduce this - I can login perfectly. I can provoke a suspicious login checkpoint challenge by doing many fail logins, but when opening the URL provided by Instaloader I can solve the challenge and login with Instaloader without any problems. Based on @e5150 comment in #217 (comment) I made the following snippet to workaround login problems by importing the session cookies from Firefox and bypassing Instaloader's login. (the linked comment also describes how to do that with Chromium)

from glob import glob
from os.path import expanduser
from sqlite3 import connect

from instaloader import ConnectionException, Instaloader

# FIREFOXCOOKIEFILE = "/home/alex/.mozilla/firefox/l96w6b90.default/cookies.sqlite"
FIREFOXCOOKIEFILE = glob(expanduser("~/.mozilla/firefox/*.default/cookies.sqlite"))[0]

instaloader = Instaloader(max_connection_attempts=1)
instaloader.context._session.cookies.update(connect(FIREFOXCOOKIEFILE)
                                            .execute("SELECT name, value FROM moz_cookies "
                                                     "WHERE baseDomain='instagram.com'"))

try:
    username = instaloader.test_login()
    if not username:
        raise ConnectionException()
except ConnectionException:
    raise SystemExit("Cookie import failed. Are you logged in successfully in Firefox?")

instaloader.context.username = username
instaloader.save_session_to_file()

To use this,

  1. login to Instagram in Firefox,
  2. execute the snippet, then
  3. instaloader -l USERNAME should work fine

If you do not use your default firefox profile, or your operating system has the paths differently set up than mine (Debian Buster with Firefox 60), you may have to alter the FIREFOXCOOKIEFILE variable first. Could the ones affected by this issue please try this out?

worked perfectly. I just copied the session generated

on Mac OS Catalina I had to do some changes:

FIREFOXCOOKIEFILE = glob(expanduser("~/.mozilla/firefox/*.default/cookies.sqlite"))[0]

into:

FIREFOXCOOKIEFILE = glob(expanduser("~/Library/Application Support/Firefox/Profiles/*.default-release/cookies.sqlite"))[0]

where *.default is unique. and:

instaloader.context._session.cookies.update(connect(FIREFOXCOOKIEFILE) .execute("SELECT name, value FROM moz_cookies " "WHERE baseDomain='instagram.com'"))

into:

instaloader.context._session.cookies.update(connect(FIREFOXCOOKIEFILE)
                                            .execute("SELECT name, value FROM moz_cookies "
                                                     "WHERE host='.instagram.com'"))
aandergr commented 4 years ago

@rocailler Thanks for your message. We have now an updated 615_import_firefox_session.py which should work with the Mac OS X paths and recent Firefox, without requiring any change.

khanfarhan10 commented 1 month ago

get_posts is causing the issue for me.

> File "src/utils/scrape_videos.py", line 117, in scrape_insta
    for post in profile.get_posts():
        │       │       └ <function Profile.get_posts at 0x7fe08a0910d0>
        │       └ <Profile comedycentral (4416358)>
        └ <Post C4OjLUhsc-5>

  File "/anaconda3/envs/social_media_automator/lib/python3.8/site-packages/instaloader/nodeiterator.py", line 160, in __next__
    query_response = self._query(self._data['page_info']['end_cursor'])
                     │    │      │    └ {'edges': [{'node': {'code': 'C4gCA8CLaGH', 'pk': '3323665385553502599', 'id': '3323665385553502599_55456973920', 'ad_id': No...
                     │    │      └ <instaloader.nodeiterator.NodeIterator object at 0x7fe08a03abe0>
                     │    └ <function NodeIterator._query at 0x7fe08a0cd1f0>
                     └ <instaloader.nodeiterator.NodeIterator object at 0x7fe08a03abe0>
  File "anaconda3/envs/social_media_automator/lib/python3.8/site-packages/instaloader/nodeiterator.py", line 106, in _query
    return self._query_doc_id(self._doc_id, after)
           │    │             │    │        └ '3318744685000904633_4416358'
           │    │             │    └ '7898261790222653'
           │    │             └ <instaloader.nodeiterator.NodeIterator object at 0x7fe08a03abe0>
           │    └ <function NodeIterator._query_doc_id at 0x7fe08a0cd280>
           └ <instaloader.nodeiterator.NodeIterator object at 0x7fe08a03abe0>
  File "anaconda3/envs/social_media_automator/lib/python3.8/site-packages/instaloader/nodeiterator.py", line 119, in _query_doc_id
    self._context.doc_id_graphql_query(
    │    │        └ <function InstaloaderContext.doc_id_graphql_query at 0x7fe08a11f9d0>
    │    └ <instaloader.instaloadercontext.InstaloaderContext object at 0x7fe08b2a2f70>
    └ <instaloader.nodeiterator.NodeIterator object at 0x7fe08a03abe0>
  File "/home/koireader/anaconda3/envs/social_media_automator/lib/python3.8/site-packages/instaloader/instaloadercontext.py", line 549, in doc_id_graphql_query
    resp_json = self.get_json('graphql/query',
                │    └ <function InstaloaderContext.get_json at 0x7fe08a11f8b0>
                └ <instaloader.instaloadercontext.InstaloaderContext object at 0x7fe08b2a2f70>
  File "anaconda3/envs/social_media_automator/lib/python3.8/site-packages/instaloader/instaloadercontext.py", line 452, in get_json
    raise QueryReturnedBadRequestException(self._response_error(resp))
          │                                │    │               └ <Response [400]>
          │                                │    └ <staticmethod object at 0x7fe08a113b20>
          │                                └ <instaloader.instaloadercontext.InstaloaderContext object at 0x7fe08b2a2f70>
          └ <class 'instaloader.exceptions.QueryReturnedBadRequestException'>

instaloader.exceptions.QueryReturnedBadRequestException: 400 Bad Request - "fail" status, message "checkpoint_required" when accessing https://www.instagram.com/graphql/query

Also instagram warned me around this. Maybe putting irregular sleeps in between would help.

khanfarhan10 commented 1 month ago

Yes, putting time.sleep(random.randint(10,50)) on iterating over each post fixes the issue for me