feat!: replace requests with httpcloak for TLS fingerprinting (#2363)#2364
feat!: replace requests with httpcloak for TLS fingerprinting (#2363)#2364iiviie wants to merge 2 commits intosubzeroid:masterfrom
Conversation
|
I have tested this httpcloak version, but it didn't work in my environment which old version worked. |
|
@ishiadv hey yeah i encountered the same issue today, changing the Instagram outdated version 269.0.0.18.75 (OnePlus 6T) to 358.0.0.47.96 (Pixel 6 Pro) at libs/instagrapi/instagrapi/mixins/auth.py seemed to fix the issue |
|
@iiviie |
|
@ishiadv yup the httpcloak preset is ios, but i tried the android and chrome presets and they didnt seem to work, also as far as i know instagrapi overrides the agent anyways. But httpcloak alteast hides the TLS signature. |
|
Good work, I did something similar too. |
|
got an issue when using it on my vps when fetching post comments, it says browser not supported |
Summary
Replaces the
requestslibrary withhttpcloakto bypass Instagram's TLS fingerprinting bot detection, fixing login and API blocking issues.Closes #2363
Problem
Instagram uses JA3/JA4 TLS fingerprinting to detect and block automated requests. Python's
requestslibrary has a distinct fingerprint that Instagram easily identifies and blocks, causing login failures and "challenge required" errors.Solution
Integrated HTTPCloak library which mimics real browser TLS fingerprints:
ios-chrome-143preset (mobile Instagram app)safari-18preset (web browser)tls_only=Truemode to maintain custom User-Agent while keeping browser TLS fingerprintMajor File Changes
Core Integration
pyproject.tomlrequests==2.32.5→httpcloak>=1.0.0instagrapi/__init__.pyrequests→httpcloakset_proxy(): Now callssession.set_proxy()method instead of settingproxiesdictinstagrapi/mixins/private.pyrequests.Session()withhttpcloak.Session(preset="ios-chrome-143", tls_only=True)get_header_value()helper for httpcloak's list-style headersinstagrapi/mixins/public.pyrequests.Session()withhttpcloak.Session(preset="safari-18", tls_only=True)instagrapi/mixins/auth.pycookie_dictproperty:cookies.get_dict()→cookies(already a dict)init(): Useset_cookie()method instead ofcookies.update()httpcloak_session_dataviamarshal()/unmarshal()inject_sessionid_to_public(): Useset_cookie()methodinstagrapi/mixins/password.pyget_header_value()import and usage for header handlingDownload Methods
instagrapi/mixins/photo.py,video.py,track.pyrequests.get()toself.private.get()for consistent fingerprintingresponse.raw→response.iter_content(chunk_size=8192)instagrapi/image_util.pyhttpcloak.Session(preset="safari-18", tls_only=True)Proxy/Cookie Handling
instagrapi/mixins/account.py,challenge.pysession.proxies→session.get_proxy()with dict conversionTests
tests.pyrequests→httpcloakhttpcloak.Session().get().requestattribute)Breaking Changes
For advanced users only (public API unchanged):
client.private.cookies.get_dict()client.private.cookiesclient.private.cookies.set("name", "val")client.private.set_cookie("name", "val")client.private.cookies.clear()client.private.clear_cookies()client.private.proxies.get("https")client.private.get_proxy()Testing
Comprehensive test suite created and verified locally:
All core functionality verified working with no Instagram blocks detected.
Documentation
README.mdwith HTTPCloak integration sectiondocs/httpcloak.md- User guide, migration instructions, and troubleshootingMigration
End users: No code changes required! Just upgrade:
pip install instagrapi --upgrade
Your existing code works as-is
Advanced users: See docs/httpcloak.md (docs/httpcloak.md) for session object changes.
Tested Presets