S
S
sergofun2014-01-07 15:25:38
Java
sergofun, 2014-01-07 15:25:38

Authorization standalone application vk on java

Good afternoon or evening!
The topic is pretty hackneyed, but I did not find specific solutions.
habrahabr.ru/post/144813
Here we have a good example:

HttpClient httpClient = new DefaultHttpClient();
// Делаем первый запрос
HttpPost post = new HttpPost("http://oauth.vk.com/authorize?" +
        "client_id="+client_id+
        "&scope="+scope+
        "&redirect_uri="+redirect_uri+
        "&display="+display+
        "&response_type="+response_type);
HttpResponse response;
response = httpClient.execute(post);
post.abort();
//Получаем редирект
String HeaderLocation = response.getFirstHeader("location").getValue();
URI RedirectUri = new URI(HeaderLocation);
//Для запроса авторизации необходимо два параметра полученных в первом запросе
//ip_h и to_h
String ip_h= RedirectUri.getQuery().split("&")[2].split("=")[1];
String to_h=RedirectUri.getQuery().split("&")[4].split("=")[1];
// Делаем запрос авторизации
post = new 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="+email+
        "&pass="+pass);
response = httpClient.execute(post);
post.abort();
// Получили редирект на подтверждение требований приложения
HeaderLocation = response.getFirstHeader("location").getValue();
post = new HttpPost(HeaderLocation);
// Проходим по нему
response = httpClient.execute(post);
post.abort();
// Теперь последний редирект на получение токена
HeaderLocation = response.getFirstHeader("location").getValue();
// Проходим по нему
post = new HttpPost(HeaderLocation);
response = httpClient.execute(post);
post.abort();
// Теперь в след редиректе необходимый токен
HeaderLocation = response.getFirstHeader("location").getValue();
// Просто спарсим его сплитами
access_token = HeaderLocation.split("#")[1].split("&")[0].split("=")[1];

but on the first line:
String HeaderLocation = response.getFirstHeader("location").getValue();
Throws a NullPointerException. In general, we need to get ip_h and to_h from these headers in order to log in further, since this method, as you understand, does not imply a browser.
The question is how to fix this and has something changed with vk authorization? Maybe someone has a ready-made example in java. Or how to emulate the work of the browser, its shell? For example, in c# there is a WebViev element, what is working now in java?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
sergofun, 2014-01-08
@sergofun

The issue is resolved, look towards javaFX and the attached javaFXSceneBuilder for FXML files, there are all the necessary components, including WebViev.

V
Vitaly, 2014-01-07
@Makute

There is a working example of authorization in java, google kate api, along with the library there is a working, beautiful and neat example of authorization, nothing has changed in it at all.

S
sergofun, 2014-01-07
@sergofun

If I'm not mistaken, then kate api is for android and there are such elements as webview, I just saw this in the kate api example
https://github.com/thest1/Android-VKontakte-SDK
I don't have this in NetBeans.
Sorry if I missed something in kate api, but I didn’t even see any such code for authorization.

E
Egor Kazantsev, 2014-01-07
@saintbyte

Do not scoff at VK like that.
You throw the usual WebView on the activity. Set him on

https://oauth.vk.com/authorize?client_id=111111111&scope=friends,notify,photos,photos,audio,video,docs,notes,pages,groups,offline&redirect_uri=https://oauth.vk.com/blank.html&display=mobile&v=5.5&response_type=token
 &revoke=1

The code is something like this:
WebView wv = (WebView) this.findViewById(R.id.webView1);
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);
        wv.setHorizontalScrollBarEnabled(false);
        webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
    wv.setWebViewClient(new VkWebViewClient());
    wv.loadUrl("https://oauth.vk.com/authorize?client_id=111111&scope=friends,notify,photos,photos,audio,video,docs,notes,pages,groups,offline&redirect_uri=https://oauth.vk.com/blank.html&display=mobile&v=5.5&response_type=token&revoke=1");

and VkWebViewClient
public class VkWebViewClient extends WebViewClient {
    public VkWebViewClient() {
      // TODO Auto-generated constructor stub
    }
  
    @Override
    public void onPageFinished(WebView view, String url)
    {
      Log.i("VkWebViewClient onPageFinished",url);
      if (url.contains("oauth.vk.com/blank.html#")) {
              if (url.contains("error")) {
            // Error
          }
          else
          {
String ahrore = url.substring(url.indexOf("#")+1);
}
}
}

and from ahrore we get user_id and token for API =)

S
sergofun, 2014-01-08
@sergofun

Everything is super, I know this and I only dream about it) Tell me where to get WebView, not for an android application, but for a regular windows desktop ?? I know what this is in android.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question