Skip to main content
I've been trying to set up Google reCaptcha v2 for our public community (Credit Application) for two weeks.  I have tried several different solutions that I found online.

I finally got the client side functionality working today, however, when I go back to the Google site, it says "We detected that your site is not verifying reCAPTCHA solutions. This is required for the proper use of reCAPTCHA on your site. Please see our developer site for more information". 

I see code out there, but don't know how/where to put this in my VF page/Lightning component. 

I cannot possibly be the only person who has this issue.  Can anyone please help???
2 件の回答
  1. 2021年6月12日 12:25

    Hi Robin,

    1. Pass the response from VF page to LWC on verify callback.

    var verifyCallback = function (response) {

          var message = {name : 'Unlock',payload : response}

           parent.postMessage(message,"{!$Site.BaseSecureUrl}");

    };

    Sample response string: 

    03AGdBq24RMWIws-TzsayYSg7D6hERdBXLVlNmKRsdeL1VuN_JzKHqE7Oz3RDaqRliCVMtc-WHHhUC0LlXpBqQHGYax3NDaD7OXx9DvE611WJkTHBUYHUI9qdDGSGmcIu8KQ5KCDok1vDv6xkkS6TpI8QBUjr1UTRdLLMcM9nu0tb1Yj1Js7RUqjv45m8RpwsEOgt30tmmmjF7P_jx6k7TPPObp6u9B2JbMQxL4s9M8bVkznxy0suHfjsnOko4WLnP0boADl0qtxU7kQGfc6kA75qIZ8JJtD3FzMT3PvOQzxQBQTq63B-RnGAk2Da3gaq-cxwgOxDo_tqVUyBUNE7Nv7g9Zn7dRL9ij80lyIIUiMrhXFxudTWfVV1yugJvbOytFlu2HQ8ZYOwvrizcj-e-Eu7kt8w2HlZGyPmyggZCQ1hw34cVz8zLMeHRO6ySKNmk6I2vKGpzo3fdPIbKZMyd_qNDsTsbUAwTcQ

    2. In LWC, on button submit-> call the method from the Apex class which will validate the response is valid or not. 

    3. Apex class method: Setup named credential or remote setting 

    @AuraEnabled

        public static String getServerResponse(String recaptchaResponse){

            Http http = new Http();

            HttpRequest request = new HttpRequest();

            request.setEndpoint('callout:Google_Recaptcha_End_Point');

            request.setMethod('POST');

            request.setBody('secret=' + recaptchaSecretKey + '&response=' + recaptchaResponse);

            HttpResponse response = http.send(request);

            if (response.getStatusCode() == 200) {

                Map<String, Object> result = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());

                if (result.containsKey('success') && result.get('success') == true) {

                    if (result.containsKey('score')) {

                       return 'Success - v3';

                    } else {

                        return 'Success - v2';

                    }

                } 

            }

            return 'Invalid Verification Request';

        }

    For test class, refer this trailhead: https://trailhead.salesforce.com/content/learn/modules/apex_integration_services/apex_integration_rest_callouts

    Please mark this as the best answer if it helps.