B
B
Boris Onofrey2014-07-18 08:40:46
Java
Boris Onofrey, 2014-07-18 08:40:46

How to pass VKontakte authorization in Java?

I am writing a client for VKontakte in Java. You need to get a token using the username and password set by the user. There are no materials on Habré and Google on this topic. Please help with a piece of code.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
andre_macareno, 2014-07-18
@RedCreepster

First, you need to create a Standalone application through a special VK form.
Secondly, if there is an opportunity and desire, you can use some kind of WebView to guide the user through all sorts of windows like entering authorization and allowing access.
But if this method is not suitable, then you can simply parse the HTML, and send POST requests, and at the very end, tear out the token from the address bar. For an Android app, I replaced # with "?" and parsed the resulting query string:

HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
                HttpHost currentHost = (HttpHost)  context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
                String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI());
                currentUrl = currentUrl.replace("#", "?");
                Uri uri=Uri.parse(currentUrl);
                String access_token = uri.getQueryParameter("access_token");
                long user_id = Long.valueOf(uri.getQueryParameter("user_id"));

Again, this is how it is done in Android - in pure Java, there are simply not enough classes for this.
But in general, this is very ugly, and it is best, of course, to beg for direct authorization.

D
DR_Demons, 2014-07-18
@DR_Demons

Read this

S
Shamil, 2014-07-18
@jawakharlal

I think those on resources like stackoverflow.com should be looked for

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question