ping / instagram_private_api

A Python library to access Instagram's private API.
MIT License
2.94k stars 608 forks source link

Web API client login throwing 400 Bad Request #382

Closed portent-andy closed 2 years ago

portent-andy commented 2 years ago

Before submitting an issue, make sure you have:

Which client are you using?


Describe the Bug/Error:

When attempting to login to an account using the web api, the library client.py file is throwing a 400 status HTTPError "Bad Request" while trying to open https://www.instagram.com/accounts/login/ajax/.

I just used this exact same code in October 2021 without issue. It feels like Instagram changed something on their end, but I'm not certain what.


Paste the output of python -V here: Python 2.7.9

Code:

# Example code that will produce the error reported
import hashlib
import string
import random
from instagram_web_api import Client, ClientCompatPatch, ClientError, ClientLoginError
import  time

class MyClient(Client):

    @staticmethod
    def _extract_rhx_gis(html):
        options = string.ascii_lowercase + string.digits
        text = ''.join([random.choice(options) for _ in range(8)])
        return hashlib.md5(text.encode()).hexdigest()

    def login(self):
        self.username = 'example@example.com'
        self.password = 'examplepassword'
        #self.on_login = True

        """Login to the web site."""
        if not self.username or not self.password:
            raise ClientError('username/password is blank')

        #time = str(int(datetime.datetime.now().timestamp()))
        #enc_password = f"#PWD_INSTAGRAM_BROWSER:0:{time}:{self.password}"
        enc_password = ("#PWD_INSTAGRAM_BROWSER:0:{}:{}".format(int(time.time()), self.password)) #python2.7

        params = {'username': self.username, 'enc_password': enc_password, 'queryParams': '{}', 'optIntoOneTap': False}
        self._init_rollout_hash()
        login_res = self._make_request('https://www.instagram.com/accounts/login/ajax/', params=params)
        if not login_res.get('status', '') == 'ok' or not login_res.get ('authenticated'):
            raise ClientLoginError('Unable to login')

        if self.on_login:
            cache_settings = login_res.settings
            with open("_instagram_settings.json", 'w') as outfile:
                json.dump(cache_settings, outfile, default=to_json)

            #on_login_callback = self.on_login
            #on_login_callback(self)
        return login_res

Error/Debug Log:


Traceback (most recent call last):
  File "test_instagram.py", line 14, in <module>
    web_api.login()
  File "C:\pythonapps\myapp\MyClient.py", line 33, in login
    login_res = self._make_request('https://www.instagram.com/accounts/login/ajax/', params=params)
  File "C:\Python27\lib\site-packages\instagram_web_api\client.py", line 289, in _make_request
    raise ClientBadRequestError(msg, e.code)
instagram_web_api.errors.ClientBadRequestError: HTTPError "Bad Request" while opening https://www.instagram.com/accounts/login/ajax/```
portent-andy commented 2 years ago

After further investigation, this seems to be authentication/security issues from Instagram. I got the original code to work using a different account. Closing this.