Skip to content

Commit 4a62fb4

Browse files
committed
Fix auth
1 parent a5f2cc5 commit 4a62fb4

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

vk_api/vk_api.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
cookies_to_list, set_cookies_from_list
2525
)
2626

27-
RE_LOGIN_HASH = re.compile(r'name="lg_h" value="([a-z0-9]+)"')
27+
RE_LOGIN_TO = re.compile(r'"to":"(.*?)"')
28+
RE_LOGIN_IP_H = re.compile(r'name="ip_h" value="([a-z0-9]+)"')
29+
RE_LOGIN_LG_H = re.compile(r'name="lg_h" value="([a-z0-9]+)"')
30+
RE_LOGIN_LG_DOMAIN_H = re.compile(r'name="lg_domain_h" value="([a-z0-9]+)"')
31+
2832
RE_CAPTCHAID = re.compile(r"onLoginCaptcha\('(\d+)'")
2933
RE_NUMBER_HASH = re.compile(r"al_page: '3', hash: '([a-z0-9]+)'")
3034
RE_AUTH_HASH = re.compile(r"Authcheck\.init\('([a-z_0-9]+)'")
@@ -33,6 +37,7 @@
3337
RE_PHONE_PREFIX = re.compile(r'label ta_r">\+(.*?)<')
3438
RE_PHONE_POSTFIX = re.compile(r'phone_postfix">.*?(\d+).*?<')
3539

40+
DEFAULT_USERAGENT = 'Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0'
3641

3742
DEFAULT_USER_SCOPE = sum(VkUserPermissions)
3843

@@ -109,10 +114,7 @@ def __init__(self, login=None, password=None, token=None,
109114

110115
self.http = session or requests.Session()
111116
if not session:
112-
self.http.headers.update({
113-
'User-agent': 'Mozilla/5.0 (Windows NT 6.1; rv:52.0) '
114-
'Gecko/20100101 Firefox/52.0'
115-
})
117+
self.http.headers['User-agent'] = DEFAULT_USERAGENT
116118

117119
self.last_request = 0.0
118120

@@ -248,14 +250,27 @@ def _vk_login(self, captcha_sid=None, captcha_key=None):
248250
# Get cookies
249251
response = self.http.get('https://vk.com/')
250252

253+
headers = {
254+
'Referer': 'https://vk.com/',
255+
'Content-Type': 'application/x-www-form-urlencoded',
256+
'Origin': 'https://vk.com',
257+
}
258+
251259
values = {
252260
'act': 'login',
253261
'role': 'al_frame',
262+
'expire': '',
263+
'to': search_re(RE_LOGIN_TO, response.text),
264+
'recaptcha': '',
265+
'captcha_sid': '',
266+
'captcha_key': '',
254267
'_origin': 'https://vk.com',
255-
'utf8': '1',
268+
'ip_h': search_re(RE_LOGIN_IP_H, response.text),
269+
'lg_h': search_re(RE_LOGIN_LG_H, response.text),
270+
'lg_domain_h': search_re(RE_LOGIN_LG_DOMAIN_H, response.text),
271+
'ul': '',
256272
'email': self.login,
257-
'pass': self.password,
258-
'lg_h': search_re(RE_LOGIN_HASH, response.text)
273+
'pass': self.password
259274
}
260275

261276
if captcha_sid and captcha_key:
@@ -265,13 +280,14 @@ def _vk_login(self, captcha_sid=None, captcha_key=None):
265280
captcha_key
266281
)
267282
)
283+
values['captcha_sid'] = captcha_sid
284+
values['captcha_key'] = captcha_key
268285

269-
values.update({
270-
'captcha_sid': captcha_sid,
271-
'captcha_key': captcha_key
272-
})
273-
274-
response = self.http.post('https://login.vk.com/', values)
286+
response = self.http.post(
287+
'https://login.vk.com/?act=login',
288+
data=values,
289+
headers=headers
290+
)
275291

276292
if 'onLoginCaptcha(' in response.text:
277293
self.logger.info('Captcha code is required')

0 commit comments

Comments
 (0)