Login with proxy using Instagrapi

1.2k views Asked by At

I wrote some code that logs in with 2fa and a proxy and then gets some info and returns it. It is not finished yet but the login section will not work. Here is the code, if someone could tell me what is wrong I would greatly appreciate it. As a side note I am using a socks5 ipv6 proxy, not sure if it must be ipv4 but I couldn't find an answer. Obviously I have removed my credentials from the code but they are put where it is indicated.

Here is my code:

from instagrapi import Client
import uuid

client_session_id = str(uuid.uuid4())

settings = {
   "uuids": {
      "phone_id": "be60e0a9-7201-5492-da04-4f2b85b3b592",
      "uuid": "be60e0a9-7201-5492-da04-4f2b85b3b592",
      "client_session_id": client_session_id,
      "advertising_id": "1c413050-795d-f321-eeb4-4962ca0a731c",
      "device_id": "android-f4f80570fe070b5f"
   },
   "cookies":  {}, 
   "last_login": 1674240519.7862692,
   "device_settings": {
      "cpu": "h1",
      "dpi": "640dpi",
      "model": "C6503",
      "device": "C6503",
      "resolution": "1794x1080",
      "app_version": "117.0.0.28.123",
      "manufacturer": "Sony",
      "version_code": "164321834",
      "android_release": "4.2.2",
      "android_version": 23
   },
   "user_agent": "Opera/9.80 (Android 4.2.2; Linux; Opera Mobi/ADR-1210241554; U; en-us) Presto/2.11.355 Version/12.10"
}


cl = Client(settings)

proxy = "socks5://username:[email protected]:30235"
cl.set_proxy(proxy)

seed = 'PUT SEED HERE'
code = cl.totp_generate_code(seed)

cl.login('USERNAME', 'PASSWORD', verification_code=code)

user_id = cl.user_id_from_username("instagram")

followers = cl.user_followers(user_id, 0)

print(followers)

cl.logout()

When I run it I get this error:

Traceback (most recent call last):
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\adapters.py", line 456, in send
    conn = self.get_connection(request.url, proxies)
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\adapters.py", line 345, in get_connection
    proxy = prepend_scheme_if_needed(proxy, "http")
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\utils.py", line 988, in prepend_scheme_if_needed
    parsed = parse_url(url)
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\urllib3\util\url.py", line 397, in parse_url
    return six.raise_from(LocationParseError(source_url), None)
  File "<string>", line 3, in raise_from
urllib3.exceptions.LocationParseError: Failed to parse: socks5://socks5://username:[email protected]:30235

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\NAME\Desktop\learning\app.py", line 41, in <module>
    cl.login('USERNAME', 'PASSWORD', verification_code=code)
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\instagrapi\mixins\auth.py", line 371, in login
    self.pre_login_flow()
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\instagrapi\mixins\auth.py", line 47, 
in pre_login_flow
    self.set_contact_point_prefill("prefill")
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\instagrapi\mixins\auth.py", line 147, in set_contact_point_prefill
    return self.private_request("accounts/contact_point_prefill/", data, login=True)
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\instagrapi\mixins\private.py", line 451, in private_request
    raise e
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\instagrapi\mixins\private.py", line 438, in private_request
    self._send_private_request(endpoint, **kwargs)
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\instagrapi\mixins\private.py", line 288, in _send_private_request
    response = self.private.post(
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 635, in post
    return self.request("POST", url, data=data, json=json, **kwargs)
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\NAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\adapters.py", line 458, in send
    raise InvalidURL(e, request=request)
requests.exceptions.InvalidURL: Failed to parse: socks5://username:[email protected]:30235
1

There are 1 answers

0
Sezer BOZKIR On

This is source code of set_proxy function:

    def set_proxy(self, dsn: str):
        if dsn:
            assert isinstance(
                dsn, str
            ), f'Proxy must been string (URL), but now "{dsn}" ({type(dsn)})'
            self.proxy = dsn
            proxy_href = "{scheme}{href}".format(
                scheme="http://" if not urlparse(self.proxy).scheme else "",
                href=self.proxy,
            )
            self.public.proxies = self.private.proxies = {
                "http": proxy_href,
                "https": proxy_href,
            }
            return True
        self.public.proxies = self.private.proxies = {}
        return False

as you can see, it seems your private proxy schema string has a problem. Check your proxy string, it should be same with: How to make python Requests work via SOCKS proxy