简体   繁体   中英

How to use google recaptcha v2 to validate form submissions in html and node.js app

I have a newsletter signup form that I want to include a recaptcha verification. I have added the widget no problem per https://developers.google.com/recaptcha/docs/display automatic display documentation. However, I have no clue how to verify the captcha on the server side, what am I supposed to do with the secret key? How do I call on the g-recaptcha-response tag? here is the code for my form:

<form class="form-signin" action="/newsletter" method="post">
  <input type="text" name="firstName" class="form-control top-form" placeholder="First Name" required autofocus>
  <input type="text" name="lastName" class="form-control middle-form" placeholder="Last Name" required>
  <input type="email" name="email" class="form-control middle-form" placeholder="Email" required>
  <input type="text" name="companyName" class="form-control bottom-form" placeholder="Company Name (if applicable)">
  <div id="label-textarea">
    <label for="message-textarea">Tell Me About Your Business:</label>
  </div>

  <textarea name="messageContent" class="form-control" rows="8" cols="80" placeholder="Include any relevant contact information that you want to display on your card (websites, social media, etc...)"></textarea>
  <div class="g-recaptcha" data-callback="recaptcha_callback" data-sitekey="6LdeRNkZAAAAAPn4PlriZzUeMsPsoOoGtcF2NeuW"></div>
  <button class="btn btn-lg btn-dark btn-block" type="submit">Claim Your Card!</button>
</form>

I know that you are supposed to have a callback function but I do not know what it is supposed to include. The docs say that you need to make a post request to the api with the secret key and response parameters, but I have no clue how to do that. If anyone could tell me how to properly integrate the google recaptcha I would greatly appreciate it!

So according to the google docs. when your user will fill recaptcha challenge and pass the test. then on the form witch you get on the server will "response" sended by post parameters, and then call with method post and send fields: your secret id and response code witch you get to:::

URL: https://www.google.com/recaptcha/api/siteverify METHOD: POST

you will get json back :

{
  "success": true|false,
  "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
  "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
  "error-codes": [...]        // optional
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM