Browse Source

Support two-factor login

Mike L 3 years ago
parent
commit
8908a5aec6
2 changed files with 21 additions and 4 deletions
  1. 19 2
      pyinstalive/auth.py
  2. 2 2
      requirements.txt

+ 19 - 2
pyinstalive/auth.py

@@ -17,12 +17,14 @@ except ImportError:
 try:
     from instagram_private_api import (
         Client, ClientError, ClientLoginError,
-        ClientCookieExpiredError, ClientLoginRequiredError)
+        ClientCookieExpiredError,
+        ClientTwoFactorRequiredError)
 except ImportError:
     sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
     from instagram_private_api import (
         Client, ClientError, ClientLoginError,
-        ClientCookieExpiredError, ClientLoginRequiredError)
+        ClientCookieExpiredError,
+        ClientTwoFactorRequiredError)
 
 
 def to_json(python_object):
@@ -65,6 +67,21 @@ def authenticate(username, password, force_use_login_args=False):
             ig_api = Client(
                 username, password,
                 on_login=lambda x: onlogin_callback(x, cookie_file), proxy=pil.proxy)
+            try:
+                ig_api.login()
+            except ClientTwoFactorRequiredError as e:
+                response = json.loads(e.error_response)
+                two_factor_info = response['two_factor_info']
+                two_factor_identifier = two_factor_info['two_factor_identifier']
+                if two_factor_info['totp_two_factor_on']:
+                    code_device = 'one-time password app'
+                else:
+                    code_device = 'number ending with {}'.format(two_factor_info['obfuscated_phone_number'])
+                verification_code = input('Enter verification code, sent to {}: '.format(code_device))
+                try:
+                    ig_api.login2fa(two_factor_identifier, verification_code)
+                except ClientError as e:
+                    print(e.error_response)
         else:
             with open(cookie_file) as file_data:
                 cached_settings = json.load(file_data, object_hook=from_json)

+ 2 - 2
requirements.txt

@@ -1,4 +1,4 @@
 argparse>=1.4.0
 configparser>=4.0.2
-instagram-private-api>=1.6.0
-instagram-private-api-extensions>=0.3.9
+git+https://github.com/botsman/instagram_private_api@master
+git+https://git@github.com/ping/instagram_private_api_extensions.git@0.3.9