Skip to content
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Examples of API requests for different captcha types are available on the [Java
- [atbCAPTCHA](#atbcaptcha)
- [CyberSiARA](#cybersiara)
- [DataDome](#datadome)
- [VK Captcha](#vk-captcha)
- [VK Image](#vk-image)
- [Other methods](#other-methods)
- [send / getResult](#send--getresult)
- [balance](#balance)
Expand Down Expand Up @@ -496,6 +498,33 @@ captcha.setUserAgent("Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHT
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");
```

### VK Image

<sup>[API method description.](https://2captcha.com/2captcha-api#vkcaptcha)</sup>

We offer two methods to solve this type of captcha - token-based and image-based.

We use the body (image in base64 format) or file (image as file) and steps parameters.
You can get both values from the response to the request https://api.vk.com/method/captchaNotRobot.getContent?v={API_VER} when loading the captcha widget on the page.

```java
VkCaptcha captcha = new VkCaptcha("vkimage");
captcha.setImageBase64(base64EncodedImage);
captcha.setSteps("[5,12,22,24,21,23,10,7,2,8,...]");
```
### VK Captcha

<sup>[API method description.](https://2captcha.com/2captcha-api#vk-captcha)</sup>

Token-based method requires redirect_uri parameter, as well as proxy and userAgent. The value of the redirect_uri parameter can be found in the response to requests to the VK API that return captchas.

```java
VkCaptcha captcha = new VkCaptcha("vkcaptcha");
captcha.setRedirectUri("https://id.vk.com/not_robot_captcha?domain=vk.com&session_token=eyJ....HGsc5B4LyvjA&variant=popup&blank=1");
captcha.setuserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36");
captcha.setProxy("http", "1.2.3.4");
```

## Other methods

### send / getResult
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/twocaptcha/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private String execute(Request request) throws Exception {

String body = response.body().string();

if (body.startsWith("ERROR_")) {
if (body.contains("ERROR_")) {
throw new ApiException(body);
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/twocaptcha/captcha/Captcha.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,11 @@ public Map<String, File> getFiles() {
return new HashMap<>(files);
}

public void setSiteKey(String siteKey) {
params.put("sitekey", siteKey);
}

public void setUrl(String url) {
params.put("pageurl", url);
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/twocaptcha/captcha/VkCaptcha.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.twocaptcha.captcha;

import java.io.File;

public class VkCaptcha extends Captcha {

public VkCaptcha(String method) {
super();
params.put("method", method);
}

public void setSteps(String steps) {
params.put("steps", steps);
}

public void setImageBase64(String imageBase64) {
params.put("body", imageBase64);
}

public void setRedirectUri(String redirect_uri) {
params.put("redirect_uri", redirect_uri);
}

public void setuserAgent(String userAgent) {
params.put("userAgent", userAgent);
}

public void setFile(String filePath) {
setFile(new File(filePath));
}

public void setFile(File file) {
files.put("file", file);
}
}
33 changes: 33 additions & 0 deletions src/main/java/examples/VkCaptchaExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package examples;

import com.twocaptcha.TwoCaptcha;
import com.twocaptcha.captcha.VkCaptcha;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class VkCaptchaExample {

private void tokenBased(String apiKey) throws IOException {
TwoCaptcha solver = new TwoCaptcha(apiKey);
VkCaptcha captcha = new VkCaptcha("vkcaptcha");
captcha.setRedirectUri("https://id.vk.com/not_robot_captcha?domain=vk.com&session_token=eyJ....HGsc5B4LyvjA&variant=popup&blank=1");
captcha.setuserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36");
captcha.setProxy("http", "1.2.3.4");

try {
solver.solve(captcha);
System.out.println("Captcha solved: " + captcha.getCode());
} catch (Exception e) {
System.out.println("Error occurred: " + e.getMessage());
}
}

public static void main(String[] args) throws Exception {
VkCaptchaExample vkExample = new VkCaptchaExample();
vkExample.tokenBased(args[0]);
}

}
37 changes: 37 additions & 0 deletions src/main/java/examples/VkImageExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package examples;

import com.twocaptcha.TwoCaptcha;
import com.twocaptcha.captcha.VkCaptcha;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class VkImageExample {

private void imageBased(String apiKey) throws IOException {
TwoCaptcha solver = new TwoCaptcha(apiKey);
solver.setExtendedResponse(1);

byte[] bytes = Files.readAllBytes(Paths.get("src/main/resources/vk.jpg"));
String base64EncodedImage = Base64.getEncoder().encodeToString(bytes);

VkCaptcha captcha = new VkCaptcha("vkimage");
captcha.setImageBase64(base64EncodedImage);
captcha.setSteps("[5,12,22,24,21,23,10,7,2,8,19,18,8,24,21,22,11,14,16,5,18,20,4,21,12,6,0,0,11,12,8,20,19,3,14,8,9,13,16,24,18,3,2,23,8,12,6,1,11,0,20,15,19,22,17,24,8,0,12,5,19,14,11,6,7,14,23,24,23,20,4,20,6,12,4,17,4,18,6,20,17,5,23,7,10,2,8,9,5,4,17,24,11,14,4,10,12,22,21,2]");

try {
solver.solve(captcha);
System.out.println("Captcha solved: " + captcha.getCode());
} catch (Exception e) {
System.out.println("Error occurred: " + e.getMessage());
}
}

public static void main(String[] args) throws Exception {
VkImageExample vkExample = new VkImageExample();
vkExample.imageBased(args[0]);
}

}
Binary file added src/main/resources/vk.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions src/test/java/com/twocaptcha/VkCaptchaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.twocaptcha;

import com.twocaptcha.captcha.VkCaptcha;
import com.twocaptcha.captcha.Yandex;

import java.util.HashMap;
import java.util.Map;

public class VkCaptchaTest extends AbstractWrapperTestCase {

public void testAllOptions() throws Exception {
VkCaptcha captcha = new VkCaptcha("vkimage");
captcha.setSiteKey("0x4AAAAAAAChNiVJM_WtShFf");
captcha.setUrl("https://ace.fusionist.io");

Map<String, String> params = new HashMap<>();
params.put("method", "vkimage");
params.put("sitekey", "0x4AAAAAAAChNiVJM_WtShFf");
params.put("pageurl", "https://ace.fusionist.io");
params.put("soft_id", "4581");
params.put("json", "0");

checkIfCorrectParamsSendAndResultReturned(captcha, params);
}

}