|
9 | 9 | import uuid
|
10 | 10 | from pathlib import Path
|
11 | 11 | from typing import Dict, List
|
| 12 | +from uuid import uuid4 |
12 | 13 |
|
13 | 14 | import requests
|
14 | 15 |
|
15 | 16 | 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 | +) |
17 | 23 | from instagrapi.utils import generate_jazoest
|
18 | 24 | from instagrapi.zones import CET
|
19 | 25 |
|
@@ -293,7 +299,7 @@ def login_by_sessionid(self, sessionid: str) -> bool:
|
293 | 299 | self.username = user.username
|
294 | 300 | return True
|
295 | 301 |
|
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: |
297 | 303 | """
|
298 | 304 | Login
|
299 | 305 |
|
@@ -346,7 +352,28 @@ def login(self, username: str, password: str, relogin: bool = False) -> bool:
|
346 | 352 | "google_tokens": "[]",
|
347 | 353 | "login_attempt_count": "0"
|
348 | 354 | }
|
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: |
350 | 377 | self.login_flow()
|
351 | 378 | self.last_login = time.time()
|
352 | 379 | return True
|
|
0 commit comments