Skip to content

Instantly share code, notes, and snippets.

@coffebar
Created November 3, 2016 14:04
Show Gist options
  • Save coffebar/fe4fb25d236f516033a0a87791515647 to your computer and use it in GitHub Desktop.
Save coffebar/fe4fb25d236f516033a0a87791515647 to your computer and use it in GitHub Desktop.
fix problems with vk.com security check
import requests
import vk
from vk.exceptions import VkAPIError
from re import match, DOTALL
from django.conf import settings
access_token = settings.VK_TOKEN
token_user_id = settings.VK_USER_ID
part_of_phone = settings.VK_SECURITY_CHECK_CODE
api = vk.API(vk.Session(access_token))
try:
api.photos.getAlbums(owner_id=token_user_id, v='5.52').get('items')
except VkAPIError as vk_error:
if vk_error.code == 17:
response = requests.get(vk_error.redirect_uri).content.decode()
data = dict(code=part_of_phone)
uri = match('.*"(/login\.php\?act=security_check[^"]+)".*', response, DOTALL)
if uri:
response = requests.post("https://m.vk.com" + uri.group(1), data=data)
if access_token in response.url:
print("Success")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment