Skip to content

Instantly share code, notes, and snippets.

@lisp3r
Created April 30, 2019 20:36
Show Gist options
  • Save lisp3r/0609c5e0524475c820366dd3b25670e2 to your computer and use it in GitHub Desktop.
Save lisp3r/0609c5e0524475c820366dd3b25670e2 to your computer and use it in GitHub Desktop.

Процедура аутентификации в VK с получением токена для веб-приложения

Осуществляем описанную в документации процедуру без редиректа пользователя в браузер.

Нам понадобится:

  • client_id=ID - ID приложения
  • login=LOGIN - логин пользователя
  • passwd=PASS - пароль пользователя
  • user_id=USR - ID пользователя
  • scopes=SCOPES - разрешения (мои разрешения: scope=notify,friends,photos,audio,video,stories,pages,status,wall,notes,messages,docs,groups,offline)

1. Инициализируем куки

$ curl -v --cookie-jar jar1 \
"https://oauth.vk.com/authorize?client_id=ID&display=mobile&redirect_uri=https://oauth.vk.com/blank.html&scope=SCOPES&response_type=token&v=5.92"

2. Отправляем логин и пароль пользоватея

Парсим пришедшую страницу в поисках ip_h и lg_h. Посылаем POST с логином и паролем:

curl -v --cookie jar1 --cookie-jar jar1 \
--data 'act=login' \
--data 'role=al_frame' \
--data '_origin=https://oauth.vk.com' \
--data 'ip_h=6c32062bbd7c1fbafb' \
--data 'lg_h=f1f91c9e42ca730423' \
--data 'email=LOGIN' \
--data 'pass=PASS' \
"https://login.vk.com/?act=login"

В ответе приходит редирект с __g_hash:

< Location: 
https://vk.com/login.php?act=slogin&to=&s=1&__q_hash=ed117fe756a39283ea0f716b4e1a2f70

3. Отправляем __g_hash

curl -v --header "accept: text/html,application/xhtml+xml,application/xmlq=0.9,image/webp,*/*q=0.8" \
--header "content-type: application/x-www-form-urlencoded" \
--cookie jar1 --cookie-jar jar1 \
"https://oauth.vk.com/authorize?client_id=ID&redirect_uri=https%3A%2F%2Foauth.vk.com%2Fblank.html&response_type=token&v=5.92&state=&display=mobile&__q_hash=ed117fe756a39283ea0f716b4e1a2f70"

В ответ приходит редирект:

< location: https://login.vk.com/?act=grant_access&client_id=ID&settings=0&redirect_uri=https%3A%2F%2Foauth.vk.com%2Fblank.html&response_type=token&group_ids=&token_type=0&v=5.92&state=&display=mobile&ip_h=6c32062bbd7c1fbafb&hash=1556654388_38de619aa1cbd614ab&https=1

4. Отправляем GET по редиректу

curl -v --header "accept: text/html,application/xhtml+xml,application/xmlq=0.9,image/webp,*/*q=0.8" \
--header "content-type: application/x-www-form-urlencoded" \
--cookie jar1 --cookie-jar jar1 \
"https://login.vk.com/?act=grant_access&client_id=ID&settings=0&redirect_uri=https%3A%2F%2Foauth.vk.com%2Fblank.html&response_type=token&group_ids=&token_type=0&v=5.92&state=&display=mobile&ip_h=6c32062bbd7c1fbafb&hash=1556654388_38de619aa1cbd614ab&https=1"

В ответ приходит редирект с токеном:

< location: https://oauth.vk.com/blank.html#access_token=1fe0ab05b0f2af4de6723ba9c72bd93ce97d9482482d26a47581b5c8915a41102d495791da09fd65f7a16&expires_in=86400&user_id=USR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment