From e014b1fe2663393dc3a48526b4b32ae4ecb88a2e Mon Sep 17 00:00:00 2001 From: python273 Date: Tue, 17 Nov 2020 23:15:41 +0300 Subject: [PATCH 1/4] Fix second factor auth --- vk_api/utils.py | 7 ++++++- vk_api/vk_api.py | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/vk_api/utils.py b/vk_api/utils.py index 82d413c6..a8448a8d 100644 --- a/vk_api/utils.py +++ b/vk_api/utils.py @@ -143,11 +143,16 @@ def send(self, request, **kwargs): total = end - start + body = request.body + if body and len(body) > 1024: + body = body[:1024] + '[STRIPPED]' + print( - '{:0.2f} {} {} {} {}'.format( + '{:0.2f} {} {} {} {} {}'.format( total, request.method, request.url, + repr(body), response.status_code, response.history ) diff --git a/vk_api/vk_api.py b/vk_api/vk_api.py index d03bbe7d..ace7c46c 100644 --- a/vk_api/vk_api.py +++ b/vk_api/vk_api.py @@ -27,9 +27,7 @@ RE_LOGIN_HASH = re.compile(r'name="lg_h" value="([a-z0-9]+)"') RE_CAPTCHAID = re.compile(r"onLoginCaptcha\('(\d+)'") RE_NUMBER_HASH = re.compile(r"al_page: '3', hash: '([a-z0-9]+)'") -RE_AUTH_HASH = re.compile( - r"\{.*?act: 'a_authcheck_code'.+?hash: '([a-z_0-9]+)'.*?\}" -) +RE_AUTH_HASH = re.compile(r"Authcheck\.init\('([a-z_0-9]+)'") RE_TOKEN_URL = re.compile(r'location\.href = "(.*?)"\+addr;') RE_PHONE_PREFIX = re.compile(r'label ta_r">\+(.*?)<') @@ -317,19 +315,28 @@ def _pass_twofactor(self, auth_response): :param auth_response: страница с приглашением к аутентификации """ - code, remember_device = self.error_handlers[TWOFACTOR_CODE]() auth_hash = search_re(RE_AUTH_HASH, auth_response.text) + if not auth_hash: + raise TwoFactorError( + 'Two factor authentication can not be passed:' + ' could not find "hash" value. Please create a bugreport' + ) + + code, remember_device = self.error_handlers[TWOFACTOR_CODE]() + values = { - 'act': 'a_authcheck_code', 'al': '1', 'code': code, - 'remember': int(remember_device), 'hash': auth_hash, + 'remember': int(remember_device), } - response = self.http.post('https://vk.com/al_login.php', values) + response = self.http.post( + 'https://vk.com/al_login.php?act=a_authcheck_code', + values + ) data = json.loads(response.text.lstrip('