function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Eric Anderson 86Eric Anderson 86 

reCaptcha and Visualforce

I am attempting to implement reCaptcha on my visual force page and regardless of the type of verification level I request (Version 2 or version 3) I get the same error message. The following is the code from my Visualforce page:
 
<apex:page controller="Captcha" cache="false">
<apex:pageBlock title="Captcha Verification">
<apex:form >
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel for="inputName" value="Name"/>
<apex:inputText value="{!myName}" id="inputName"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel for="inputEmail" value="Email"/>
<apex:inputText value="{!myEmail}" id="inputEmail"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{! NOT(verified)}">
<script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k={!publicKey}">
</script>
<noscript>
<iframe src="https://www.google.com/recaptcha/api/noscript?k={!publicKey}" height="300" width="500" frameborder="0">
</iframe><br/>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
</noscript>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton action="{!verify}" value="Check If I’m Human" rendered="{! NOT(verified)}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!verified}">
<p>reCAPTCHA verification suggests that you’re not a ‘bot.</p>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:form>
</apex:pageBlock>
</apex:page>
The following is my Apex code:
public class Captcha {
    public boolean checkCurrentValue{get;set;}
    public String challenge{get;set;}
    public String response{get;set;}
    public String publicKey {get;set;}
    private static String privateKey = '6Lf_E3sUAAAAAD936kuwlhIPCBua-oj7zWVZANg7';
    // change this key to the key which we you have  generated after click on server side integration which is “secret”
    private static String baseUrl = 'https://www.google.com/recaptcha/api/siteverify';
    public String myName { get; set; }
    public String myEmail { get; set; }
    public Boolean verified { get; private set; }
    public Captcha()
    {
        publicKey = '6Lf_E3sUAAAAAMNCGpOZ-VtGCNyQI641CrEccALh';
// we can get this key when we click on client site integration with “data-sitekey=”6LeHD3kUAAAAAL2tHSW8jgnaUQBDvEpOb6y0iUOJ”
        this.verified = false;
        checkCurrentValue=false;
        challenge = ApexPages.currentPage().getParameters().get('recaptcha_challenge_field');
        response =  ApexPages.currentPage().getParameters().get('recaptcha_response_field');
    }
    public PageReference verify()
    {
        System.debug('reCAPTCHA verification attempt');
        // On first page load, form is empty, so no request to make yet
        if ( challenge == null || response == null )
        {
            System.debug('reCAPTCHA verification attempt with empty form');
            return null;
        }
        HttpResponse r = makeRequest(baseUrl,'privatekey=' + privateKey + '&remoteip='  + remoteHost + '&challenge=' + challenge + '&response='  + response);
        if ( r!= null )
        {
            this.verified = (r.getBody().startsWithIgnoreCase('true'));
        }
        if(this.verified)
        {
            // If they pass verification, you might do something interesting here
            // Or simply return a PageReference to the “next” page
            return null;
        }
        else
        {
            // stay on page to re-try reCAPTCHA
            return null;
        }
    }
    public PageReference reset()
    {
        return null;
    }  
    /* Helper methods */
    private static HttpResponse makeRequest(string url, string body) 
    {
        HttpResponse response = null;
        HttpRequest req = new HttpRequest();  
        req.setEndpoint(url);
        req.setMethod('POST');
        req.setBody (body);
        try
        {
            Http http = new Http();
            response = http.send(req);
            System.debug('reCAPTCHA response: ' + response);
            System.debug('reCAPTCHA body: ' + response.getBody());
        }
        catch(System.Exception e)
        {
            System.debug('ERROR: ' + e);
        }
        return response;
    }
    private String remoteHost
    {
        get {
            String ret = '127.0.0.1';
            // also could use x-original-remote-host
            Map<String, String> hdrs = ApexPages.currentPage().getHeaders();
            if (hdrs.get('x-original-remote-addr')!= null)
                ret =  hdrs.get('x-original-remote-addr');
            else if (hdrs.get('X-Salesforce-SIP')!= null)
                ret =  hdrs.get('X-Salesforce-SIP');
            return ret;
        }
    } 

}
Since I am still in 'development' in my sandbox, the domain that I entered was

cdcrca--ombudsman.cs33.my.salesforce.com

The error message I am getting is the following (even though as I said, I have tried version 2 and 3 and 1 is not even available any more:
Image of error message I am getting.

Any assistance would be greatly appreciated.

Thank you! - Eric -

 
SandhyaSandhya (Salesforce Developers) 

Hi,

Try in different browser(chrome) and check also make sure that it is upgraded to V2.

Best Regards,

Sandhya

Eric Anderson 86Eric Anderson 86
Hi there Sandhya,

I get the same result regardless of the browser I use (I.E., Chrome or Edge).

I am unsure about what you mean by your comment '... also make sure that it is upgraded to V2.' When I initially went out to the Google recaptcha page, I tried selecting both V2 and V3 and neither of those options seemed to give me a valid version when I launched the page. Is there something code wise that I need to do to my Visualforce or Apex code?

Let me know. Thank you! - Eric -