1212import re
1313import threading
1414import time
15+ from urllib .parse import urlparse , parse_qs
1516
1617import requests
1718import six
3738DEFAULT_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+
4045class 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