Send direct message on Instagram using Instagrapi Python

803 views Asked by At

I'm trying to use the Instagrapi Python library to send a DM to a user on Instagram. Here is the code I'm using:

from instagrapi import Client

username = "username"
password = "password"
arrayOfUsers = [1234567890] * 1

cl = Client()
cl.login(username, password)
cl.direct_send('Hello', arrayOfUsers) #direct_send(text: str, user_ids: List[int] = [], thread_ids: List[int] = [])

However, I'm getting the following error:

Status 429: Too many requests
Ignore 429: Continue login
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagrapi\mixins\private.py", line 302, in _send_private_request
    response.raise_for_status()
  File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://i.instagram.com/api/v1/accounts/login/

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagrapi\mixins\private.py", line 438, in private_request
    self._send_private_request(endpoint, **kwargs)
  File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagrapi\mixins\private.py", line 334, in _send_private_request
    raise ChallengeRequired(**last_json)
instagrapi.exceptions.ChallengeRequired: challenge_required

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\USER\Desktop\sid PY\sid3.py", line 8, in <module>
    cl.login(username, password)
  File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagrapi\mixins\auth.py", line 390, in login
    logged = self.private_request("accounts/login/", data, login=True)
  File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagrapi\mixins\private.py", line 449, in private_request
    self.challenge_resolve(self.last_json)
  File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagrapi\mixins\challenge.py", line 78, in challenge_resolve
    return self.challenge_resolve_simple(challenge_url)
  File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\instagrapi\mixins\challenge.py", line 425, in challenge_resolve_simple
    raise ChallengeUnknownStep(f'ChallengeResolve: Unknown step_name "{step_name}" for "{self.username}" in challenge resolver: {self.last_json}')
instagrapi.exceptions.ChallengeUnknownStep: ChallengeResolve: Unknown step_name "select_contact_point_recovery" for "MYIGUSERNAME" in challenge resolver: {'step_name': 'select_contact_point_recovery', 'step_data': {'choice': '1', 'email': '[email protected]', 'hl_co_enabled': False, 'sigp_to_hl': False}, 'flow_render_type': 3, 'bloks_action': 'com.instagram.challenge.navigation.take_challenge', 'nonce_code': 'NONCE_CODE', 'user_id': USER_ID, 'cni': USER_CNI, 'challenge_context': '{"step_name": "select_contact_point_recovery", "nonce_code": "NONCE_CODE", "user_id": USER_ID, "cni": USER_CNI, "is_stateless": false, "challenge_type_enum": "HACKED_LOCK", "present_as_modal": false}', 'challenge_type_enum_str': 'HACKED_LOCK', 'status': 'ok'}

Why do I get Status 429: Too many requests when I login only once? Can someone please explain what this error means and how I can fix it? Thank you!

0

There are 0 answers