Skip to content

Commit 74da398

Browse files
committed
Support for 2FA connection [#76] Implemented two_factor_login
1 parent 3b7fe62 commit 74da398

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ from instagrapi import Client
5252
from instagrapi.types import Location, StoryMention, StoryLocation, StoryLink, StoryHashtag
5353

5454
cl = Client()
55-
cl.login(USERNAME, PASSWORD)
55+
cl.login(USERNAME, PASSWORD, verification_code='123456') # with 2FA verification_code
5656

5757
media_path = cl.video_download(
5858
cl.media_pk_from_url('https://www.instagram.com/p/CGgDsi7JQdS/')

docs/usage-guide/interactions.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
``` python
3434
cl.login("instagrapi", "42")
35+
# cl.login("instagrapi", "42", verification_code="123456") # with 2FA verification_code
3536
# cl.login_by_sessionid("peiWooShooghahdi2Eip7phohph0eeng")
3637
cl.set_proxy("socks5://127.0.0.1:30235")
3738
# cl.set_proxy("http://username:[email protected]:8080")
@@ -42,6 +43,7 @@ print(cl.user_info(cl.user_id))
4243
```
4344

4445
* login(username: str, password: str): bool - Login by username and password (get new cookies if it does not exist in settings)
46+
* login(username, password, verification_code='<2FA CODE>') - Login by username and password with 2FA verification code
4547
* relogin(): bool - Re-login with clean cookies (required cl.username/cl.password)
4648
* login_by_sessionid(sessionid: str): bool - Login by sessionid from Instagram site
4749
* get_settings(): dict - Return settings dict

instagrapi/mixins/auth.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
import uuid
1010
from pathlib import Path
1111
from typing import Dict, List
12+
from uuid import uuid4
1213

1314
import requests
1415

1516
from instagrapi import config
16-
from instagrapi.exceptions import PrivateError, PleaseWaitFewMinutes, ReloginAttemptExceeded
17+
from instagrapi.exceptions import (
18+
PleaseWaitFewMinutes,
19+
PrivateError,
20+
ReloginAttemptExceeded,
21+
TwoFactorRequired,
22+
)
1723
from instagrapi.utils import generate_jazoest
1824
from instagrapi.zones import CET
1925

@@ -293,7 +299,7 @@ def login_by_sessionid(self, sessionid: str) -> bool:
293299
self.username = user.username
294300
return True
295301

296-
def login(self, username: str, password: str, relogin: bool = False) -> bool:
302+
def login(self, username: str, password: str, relogin: bool = False, verification_code: str = '') -> bool:
297303
"""
298304
Login
299305
@@ -346,7 +352,28 @@ def login(self, username: str, password: str, relogin: bool = False) -> bool:
346352
"google_tokens": "[]",
347353
"login_attempt_count": "0"
348354
}
349-
if self.private_request("accounts/login/", data, login=True):
355+
try:
356+
logged = self.private_request("accounts/login/", data, login=True)
357+
except TwoFactorRequired as e:
358+
if not verification_code.strip():
359+
raise TwoFactorRequired(f'{e} (you did not provide verification_code for login method)')
360+
two_factor_identifier = self.last_json.get(
361+
'two_factor_info', {}
362+
).get('two_factor_identifier')
363+
data = {
364+
"verification_code": verification_code,
365+
"phone_id": self.phone_id,
366+
"_csrftoken": self.token,
367+
"two_factor_identifier": two_factor_identifier,
368+
"username": username,
369+
"trust_this_device": "0",
370+
"guid": self.uuid,
371+
"device_id": self.device_id,
372+
"waterfall_id": str(uuid4()),
373+
"verification_method": "3"
374+
}
375+
logged = self.private_request("accounts/two_factor_login/", data, login=True)
376+
if logged:
350377
self.login_flow()
351378
self.last_login = time.time()
352379
return True

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
setup(
3232
name='instagrapi',
33-
version='1.6.5',
33+
version='1.7.0',
3434
author='Mikhail Andreev',
3535
author_email='[email protected]',
3636
license='MIT',

0 commit comments

Comments
 (0)