Skip to content

Commit c5b1c31

Browse files
committed
Pass challenge.html
1 parent 65acb43 commit c5b1c31

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

vk_api/vk_api.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
RE_PHONE_PREFIX = re.compile(r'label ta_r">\+(.*?)<')
4040
RE_PHONE_POSTFIX = re.compile(r'phone_postfix">.*?(\d+).*?<')
4141

42-
DEFAULT_USERAGENT = 'Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0'
42+
DEFAULT_USERAGENT = 'Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0'
4343

4444
DEFAULT_USER_SCOPE = sum(VkUserPermissions)
4545

@@ -188,7 +188,6 @@ def auth(self, reauth=False, token_only=False):
188188
self._auth_cookies(reauth=reauth)
189189

190190
def _auth_cookies(self, reauth=False):
191-
192191
if reauth:
193192
self.logger.info('Auth forced')
194193

@@ -213,7 +212,6 @@ def _auth_cookies(self, reauth=False):
213212
self.logger.info('access_token from config is valid')
214213

215214
def _auth_token(self, reauth=False):
216-
217215
if not reauth and self._check_token():
218216
self.logger.info('access_token from config is valid')
219217
return
@@ -229,6 +227,16 @@ def _auth_token(self, reauth=False):
229227
self._vk_login()
230228
self._api_login()
231229

230+
def _check_challenge(self, response):
231+
if not response.url.startswith('https://vk.com/challenge.html?'):
232+
return response
233+
234+
hash429 = urllib.parse.parse_qs(response.url.split('?', 1)[-1])['hash429'][0]
235+
salt = re.search(r"salt\s*=\s*'(.*)'", response.text).group(1)
236+
hash429_md5 = md5(hash429.encode('ascii') + b':' + salt.encode('ascii')).hexdigest()
237+
response = self.http.get(f'{response.url}&key={hash429_md5}')
238+
return response
239+
232240
def _vk_login(self, captcha_sid=None, captcha_key=None):
233241
""" Авторизация ВКонтакте с получением cookies remixsid
234242
@@ -250,10 +258,13 @@ def _vk_login(self, captcha_sid=None, captcha_key=None):
250258
response = self.http.get('https://vk.com/login')
251259

252260
if response.url.startswith('https://vk.com/429.html?'):
261+
# is this version still used???
253262
hash429_md5 = md5(self.http.cookies['hash429'].encode('ascii')).hexdigest()
254263
self.http.cookies.pop('hash429')
255264
response = self.http.get(f'{response.url}&key={hash429_md5}')
256265

266+
response = self._check_challenge(response)
267+
257268
headers = {
258269
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
259270
'Accept-Language': 'en-US,en;q=0.5',
@@ -291,6 +302,8 @@ def _vk_login(self, captcha_sid=None, captcha_key=None):
291302
headers=headers
292303
)
293304

305+
response = self._check_challenge(response)
306+
294307
if 'onLoginCaptcha(' in response.text:
295308
self.logger.info('Captcha code is required')
296309

0 commit comments

Comments
 (0)