Last active
March 9, 2023 20:38
-
-
Save aNNiMON/483434f042fadb397eaa to your computer and use it in GitHub Desktop.
VK API sample implementation for Java 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Desktop; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.URISyntaxException; | |
import java.net.URL; | |
import java.nio.charset.StandardCharsets; | |
import java.util.HashMap; | |
/** | |
* VK API sample implementation for Java 8. | |
* @see https://github.com/liosha2007/vkontakte-api | |
* @author liosha2007 | |
* @author aNNiMON | |
*/ | |
public final class VkApi { | |
private static final String API_VERSION = "5.21"; | |
private static final String AUTH_URL = "https://oauth.vk.com/authorize" | |
+ "?client_id={APP_ID}" | |
+ "&scope={PERMISSIONS}" | |
+ "&redirect_uri={REDIRECT_URI}" | |
+ "&display={DISPLAY}" | |
+ "&v={API_VERSION}" | |
+ "&response_type=token"; | |
private static final String API_REQUEST = "https://api.vk.com/method/{METHOD_NAME}" | |
+ "?{PARAMETERS}" | |
+ "&access_token={ACCESS_TOKEN}" | |
+ "&v=" + API_VERSION; | |
public static VkApi with(String appId, String accessToken) throws IOException { | |
return new VkApi(appId, accessToken); | |
} | |
private final String accessToken; | |
private VkApi(String appId, String accessToken) throws IOException { | |
this.accessToken = accessToken; | |
if (accessToken == null || accessToken.isEmpty()) { | |
auth(appId); | |
throw new Error("Need access token"); | |
} | |
} | |
private void auth(String appId) throws IOException { | |
String reqUrl = AUTH_URL | |
.replace("{APP_ID}", appId) | |
.replace("{PERMISSIONS}", "photos,messages") | |
.replace("{REDIRECT_URI}", "https://oauth.vk.com/blank.html") | |
.replace("{DISPLAY}", "page") | |
.replace("{API_VERSION}", API_VERSION); | |
try { | |
Desktop.getDesktop().browse(new URL(reqUrl).toURI()); | |
} catch (URISyntaxException ex) { | |
throw new IOException(ex); | |
} | |
} | |
public String getDialogs() throws IOException { | |
return invokeApi("messages.getDialogs", null); | |
} | |
public String getHistory(String userId, int offset, int count, boolean rev) throws IOException { | |
return invokeApi("messages.getHistory", Params.create() | |
.add("user_id", userId) | |
.add("offset", String.valueOf(offset)) | |
.add("count", String.valueOf(count)) | |
.add("rev", rev ? "1" : "0")); | |
} | |
public String getAlbums(String userId) throws IOException { | |
return invokeApi("photos.getAlbums", Params.create() | |
.add("owner_id", userId) | |
.add("photo_sizes", "1") | |
.add("thumb_src", "1")); | |
} | |
private String invokeApi(String method, Params params) throws IOException { | |
final String parameters = (params == null) ? "" : params.build(); | |
String reqUrl = API_REQUEST | |
.replace("{METHOD_NAME}", method) | |
.replace("{ACCESS_TOKEN}", accessToken) | |
.replace("{PARAMETERS}&", parameters); | |
return invokeApi(reqUrl); | |
} | |
private static String invokeApi(String requestUrl) throws IOException { | |
final StringBuilder result = new StringBuilder(); | |
final URL url = new URL(requestUrl); | |
try (InputStream is = url.openStream()) { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); | |
reader.lines().forEach(result::append); | |
} | |
return result.toString(); | |
} | |
private static class Params { | |
public static Params create() { | |
return new Params(); | |
} | |
private final HashMap<String, String> params; | |
private Params() { | |
params = new HashMap<>(); | |
} | |
public Params add(String key, String value) { | |
params.put(key, value); | |
return this; | |
} | |
public String build() { | |
if (params.isEmpty()) return ""; | |
final StringBuilder result = new StringBuilder(); | |
params.keySet().stream().forEach(key -> { | |
result.append(key).append('=').append(params.get(key)).append('&'); | |
}); | |
return result.toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I suggest to update invokeApi:
...
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
...