⚙️ Prerequisites
- Proxy (Optional)
- Rust installed
- Capsolver API key
Step 1: Install Necessary Dependencies
Make sure your Cargo.toml
contains the necessary dependencies:
[dependencies]
reqwest = { version = "0.11", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }
👨💻 Step 2: Rust Code for solving reCaptcha v2 without Proxy
use reqwest::Client;
use serde_json::json;
use std::error::Error;
use tokio::time::{sleep, Duration};
const PAGE_URL: &str = "YourWebsiteURL"; // Replace with your Website URL
const SITE_KEY: &str = "YourSiteKey"; // Replace with your Site Key
const CLIENT_KEY: &str = "YourCapsolverAPIKey"; // Replace with your CapSolver API Key
// Create a task using the CapSolver API
async fn create_task(payload: serde_json::Value) -> Result<serde_json::Value, Box<dyn Error>> {
let client = Client::new();
let res = client
.post("https://api.capsolver.com/createTask")
.json(&json!({
"clientKey": CLIENT_KEY,
"task": payload
}))
.send()
.await?;
let data = res.json::<serde_json::Value>().await?;
Ok(data)
}
// Get the task result for the provided task ID
async fn get_task_result(task_id: &str) -> Result<serde_json::Value, Box<dyn Error>> {
let client = Client::new();
loop {
sleep(Duration::from_secs(1)).await;
println!("Getting task result for task ID: {}", task_id);
let res = client
.post("https://api.capsolver.com/getTaskResult")
.json(&json!({
"clientKey": CLIENT_KEY,
"taskId": task_id
}))
.send()
.await?;
let data = res.json::<serde_json::Value>().await?;
if data["status"] == "ready" {
return Ok(data);
}
}
}
// Solves reCaptcha by creating a task and retrieving the result
async fn solve_recaptcha(page_url: &str, site_key: &str) -> Result<serde_json::Value, Box<dyn Error>> {
let task_payload = json!({
"type": "ReCaptchaV2TaskProxyless",
"websiteURL": page_url,
"websiteKey": site_key
});
let task_data = create_task(task_payload).await?;
get_task_result(task_data["taskId"].as_str().unwrap()).await
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let response = solve_recaptcha(PAGE_URL, SITE_KEY).await?;
if let Some(token) = response["solution"]["gRecaptchaResponse"].as_str() {
println!("Received token: {}", token);
} else {
println!("Failed to retrieve token.");
}
Ok(())
}
Explanation:
create_task: Sends a request to CapSolver to create a reCaptcha solving task. The task payload is sent as JSON, and the response (containing the task ID) is returned.
get_task_result: Polls the CapSolver API every second to retrieve the result of the created task. Once the task is marked as "ready", it returns the task solution.
solve_recaptcha: Combines both the creation and result-checking logic into a single function to solve the reCaptcha.
main: This function invokes solve_recaptcha and prints out the reCaptcha response token once it's available.