Skip to content

Commit 6f8652c

Browse files
committed
Fix auth
1 parent d49c1dc commit 6f8652c

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

vk_api/vk_api.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import re
1313
import threading
1414
import time
15+
from urllib.parse import urlparse, parse_qs
1516

1617
import requests
1718
import six
@@ -37,6 +38,10 @@
3738
DEFAULT_USER_SCOPE = sum(VkUserPermissions)
3839

3940

41+
def get_unknown_exc_str(s):
42+
return 'Unknown error ({}). Please send bugreport to GitHub or vk_api@python273.pw'.format(s)
43+
44+
4045
class VkApi(object):
4146
"""
4247
:param login: Логин ВКонтакте (лучше использовать номер телефона для
@@ -301,9 +306,7 @@ def _vk_login(self, captcha_sid=None, captcha_key=None):
301306
self.storage.cookies = cookies_to_list(self.http.cookies)
302307
self.storage.save()
303308
else:
304-
raise AuthError(
305-
'Unknown error. Please send bugreport to vk_api@python273.pw'
306-
)
309+
raise AuthError(get_unknown_exc_str('AUTH; no sid'))
307310

308311
response = self._pass_security_check(response)
309312

@@ -319,10 +322,7 @@ def _pass_twofactor(self, auth_response):
319322
auth_hash = search_re(RE_AUTH_HASH, auth_response.text)
320323

321324
if not auth_hash:
322-
raise TwoFactorError(
323-
'Two-factor authentication can not be passed:'
324-
' could not find "hash" value. Please send a bugreport'
325-
)
325+
raise TwoFactorError(get_unknown_exc_str('2FA; no hash'))
326326

327327
code, remember_device = self.error_handlers[TWOFACTOR_CODE]()
328328

@@ -350,10 +350,7 @@ def _pass_twofactor(self, auth_response):
350350
elif status == '2':
351351
raise TwoFactorError('Recaptcha required')
352352

353-
raise TwoFactorError(
354-
'Two-factor authentication can not be passed.'
355-
' Please send a bugreport'
356-
)
353+
raise TwoFactorError(get_unknown_exc_str('2FA; unknown status'))
357354

358355
def _pass_security_check(self, response=None):
359356
""" Функция для обхода проверки безопасности (запрос номера телефона)
@@ -446,8 +443,19 @@ def _api_login(self):
446443
response = self.http.get(url)
447444

448445
if 'access_token' in response.url:
449-
params = response.url.split('#', 1)[1].split('&')
450-
token = dict(param.split('=', 1) for param in params)
446+
parsed_url = urlparse(response.url)
447+
parsed_query = parse_qs(parsed_url.query)
448+
449+
if 'authorize_url' not in parsed_query:
450+
raise AuthError(get_unknown_exc_str('API AUTH; no authorize_url'))
451+
452+
parsed_url = urlparse(parsed_query['authorize_url'][0])
453+
parsed_query = parse_qs(parsed_url.fragment)
454+
455+
token = {k: v[0] for k, v in parsed_query.items()}
456+
457+
if not isinstance(token.get('access_token'), str):
458+
raise AuthError(get_unknown_exc_str('API AUTH; no access_token'))
451459

452460
self.token = token
453461

@@ -619,7 +627,7 @@ def method(self, method, values=None, captcha_sid=None, captcha_key=None,
619627

620628
response = self.http.post(
621629
'https://api.vk.com/method/' + method,
622-
values
630+
values,
623631
)
624632
self.last_request = time.time()
625633

0 commit comments

Comments
 (0)