Bot for VK in Java


I am not well versed in web programming, so please do not take my possibly stupid questions to heart :)

We plan to write an application that can work with personal messages (VKontakte pages, or communities), as well as with other functionality of the VK API.

On ruSO, I saw questions about authorization using Java in VKontakte, but I did not find a detailed description of how to work with it. The VK documentation has all the necessary information information on working with the API, but there are mostly JSON requests, you need to have a server that will accept and process these requests, and so on (https://vk.com/dev/callback_api, https://vk.com/dev/bots_docs)

But how to work with this from a desktop application, for example, from my computer on Windows, I did not understand.

I won't have any problems writing the app, but how to work with the VK API and interact with it (send messages, or perform any other actions) I don't know.

It is desirable that you can:

  • Store tokens so that they are stored for as long as possible, and there is no need to log in again (log in through a window in the application - embedding a web page is not so difficult, but how do I get the bot to remain logged in?)
  • Process any requests that VK sends, as well as send them yourself (same messages / any message). interaction)

I will be glad to receive any tips. I would be even more grateful if you provide an example of how to log in and store this authorization, how to interact with the API (the simplest examples).

Author: Pavel Vergeev, 2017-03-12

2 answers

The Java SDK from VK itself is almost certainly suitable for your purpose. The SDK page contains detailed documentation (and, in particular, instructions for obtaining the user's access token).

Before you dive into the SDK, it is better to read about how the Vkontakte API works (this is a small article).

Most likely, you need a standalone application. It is normal for them to use https://oauth.vk.com/blank.html as REDIRECT_URI (this is even mandatory for some API methods).

To make the access token live forever, add the value offline to scope.


The API without the SDK is interacted with like this:

// формируют url запроса
String url = "https://api.vk.com/method/messages.get?count=20&access_token=<access_token>";

URL obj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();

// из документации: параметры могут передаваться как методом GET, так и POST. Если вы будете передавать большие данные (больше 2 килобайт), следует использовать POST. 
connection.setRequestMethod("GET");
// посылаем запрос и сохраняем ответ
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();
// выведет json-ответ запроса
System.out.println(response.toString());

(the get request code took from here) And then you need to parse the received json-response.

Authorization is more difficult. There you need to open a browser window with the generated url, detect the redirect, and parse access_token in a new url.


A good exercise would be to look at the sources of Java SDK and understand how it is implemented there. For example, the execution of message.get starts with here.

 7
Author: Pavel Vergeev, 2017-03-12 15:06:30

You can use my library.

To log in, you will need to get an access token in the VK. The rest is done in 2 minutes. VkApiProvider on github, there's even an example implementation.

If you have any questions, please contact us.

 0
Author: JavaStream, 2021-01-06 13:19:36