In this post we want to show you the code for an automatic connection to 2captcha service for solving google reCaptcha v2.0. Not long ago, google drastically complicated the user-behavior reCaptcha (v2.0). This online service provides a method for solving it.

The simple algorithm this service uses is as follows:

  1. The target site open credentials (recaptcha’s “site key“, site url, optional: proxy IP) are copied by you (client) and submitted to the 2captcha service. You find them using simple web developer tools.
  2. A worker at the service’s end solves reCaptcha with the provided credentials.
  3. In 10-30 seconds you request an answer as a g-recaptcha-response token.
  4. You use this g-recaptcha-response token inside of the target site [submit] form with recaptcha.
Note: All these steps can be done without imitating a browser, just by pure http GET and POST requests, and we show how.

For the purpose of illustration, we made a testing ground with recaptcha.

Get credentials

2captcha service requires us to provide it with the following parameters:

 Request parameter Value
key SERVICE_KEY (2 captchas service key)
googlekey data-sitekey attribute value in g-recaptcha block element
pageurl http://testing-ground.scraping.pro/recaptcha (url of a target page with recaptcha)
method userrecaptcha

 

Now we go to the site page and inspect the recaptcha html code in web developer tools (hit F12). We find and get the data-sitekey attribute value in the g-recaptcha block. Its value is a constant for a single site, the site_key value provided by google. See a value highlighted in blue on the shot below:2captcha-get-site-key

So we select it and right-click to copy.

Now we have gotten the googlekey parameter (site’s google site key ): 6Lf5CQkTAAAAAKA-kgNm9mV6sgqpGmRmRMFJYMz8

SERVICE_KEY for the following requesting is taken from the 2captcha account settings:2captcha-get-captcha-api-key

 

Submit to service a request for recaptcha solution

Now we make a GET or POST request to the 2captcha service (in.php endpoint) with the above-mentioned parameters:

http://2captcha.com/in.php?key=SERVICE_KEY&method=userrecaptcha&googlekey=6Lf5CQkTAAAAAKA-kgNm9mV6sgqpGmRmRMFJYMz8&pageurl=http://testing-ground.scraping.pro/recaptcha

Python code:

The 2captcha service renders a response in the form of: OK|Captcha_ID where Captcha_ID – is the id of the recaptcha in the system.

Receive valid token

Now we need to wait till a worker solves the recaptcha and google returns a valid token to the service. For this we can make a request to the 2captcha service every 5 seconds until we get a valid token. See a request to res.php endpoint with parameters:

http://2captcha.com/res.php?key=SERVICE_KEY&action=get&id=Captcha_ID

Python code

Submit google’s token in form

Now we submit the form with the g-recaptcha-response token.

At the target site (server-side), this token is checked. The site’s script sends a request to google to check the  g-recaptcha-response token’s validity: if it is true or not, pertaining to that site or not, etc. At our Captcha testing ground this token is checked before the form submission. It is done by passing a token through ajax (xhr) request to proxy.php which, in turn, inquires of google if the site is verified and returns google’s response.

proxy.php

See the post on how to insert and configure reCaptcha code in php.

Python code to send g-recaptcha-response to proxy.php for site verification by google

The script should result in a json:

{ “success”: true,
“challenge_ts”: “2016-09-29T09:25:55Z”,
“hostname”: “testing-ground.scraping.pro”

}

Python code of a form submitting with g-recaptcha-response:

The whole code

Limitations

The received g-recaptcha-response token (from 2captcha service) is valid for only 120 seconds (2 min), so you are responsible to apply it in the target site [submit] form within that time limit.

Other language solutions

You might also look at other language options for how to apply 2captcha service:

  • C# code  (code for the same testing-ground page)
  • Java example (with Russian comments only)

Fast recaptcha solutions for bulk form submit

Now the average service solution time is 25 seconds. In the following post we share how to optimize the recaptcha solution with regards to buck solving of the recaptchas with this service.