K
K
Konstantin Dovnar2016-11-14 18:19:22
Java
Konstantin Dovnar, 2016-11-14 18:19:22

VK login using Kotlin/Java?

In this post (which, unfortunately, is already in 2012), the author managed to write a function that itself received an Access Token.
Now we need to do the same.
The function on Kotlin that is available (full analogue from the post):

//Здесь добавлено, дабы не было ошибки при работе с cookie
    val globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build()
    val httpClient = HttpClientBuilder.create().setDefaultRequestConfig(globalConfig).build()
    var post = HttpPost(
                    "http://oauth.vk.com/authorize?" +
                    "client_id=" + id +
                    "&scope=" + "wall,photos" +
                    "&redirect_uri=" + "https://oauth.vk.com/blank.html" +
                    "&display=" + "popup" +
                    "&response_type=" + "token"
    )
    var response = httpClient.execute(post)
    post.abort()

// Тут ошибка NullPointerException
    var headerLocation = response.getFirstHeader("location").value
    val uri = URI(headerLocation)

    val ip_h = uri.query.split("&")[2].split("=")[1]
    val to_h = uri.query.split("&")[4].split("=")[1]

    post = HttpPost(
                    "https://login.vk.com/?act=login&soft=1"+
                    "&q=1"+
                    "&ip_h="+ip_h+
                    "&from_host=oauth.vk.com"+
                    "&to="+to_h+
                    "&expire=0"+
                    "&email="+ login +
                    "&pass="+ pass
    )
    response = httpClient.execute(post)
    post.abort()
    headerLocation = response.getFirstHeader("location").value

    post = HttpPost(headerLocation)
    response = httpClient.execute(post)
    post.abort()

    headerLocation = response.getFirstHeader("location").value

    httpClient.close()

    return headerLocation.split("#")[1].split("&")[0].split("=")[1]

But the problem is that already after the first post request, there is no header Location in the response (I marked the place of the error with a comment), although if you copy the line and paste it into the browser, everything is fine.
Is it possible to get around this somehow?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kosarev, 2016-11-15
@jaxtr

There are already ready-made solutions like Spring Social VKontakte .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question