Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: reCaptcha Google


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Mar 27, 2017 11:01 PM Last Post By: Johan Erasmus
Frederick Couto

Posts: 9
Registered: 12/17/09
reCaptcha Google  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 1, 2017 12:58 PM
Hello guys,

I would like information on how to use the reCaptcha procedure that is made available by Google in my intraweb project.

My difficulty is in the test or not.

I use Delphi 10 Seattle with IntraWe3b 14.0.63.

Thank you in advance for the attention of all my colleagues.

Edited by: Frederick Couto on Feb 1, 2017 1:04 PM
Johan Erasmus

Posts: 4
Registered: 6/11/06
Re: reCaptcha Google  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 27, 2017 5:40 AM   in response to: Frederick Couto in response to: Frederick Couto
Frederick Couto wrote:
Hello guys,

I would like information on how to use the reCaptcha procedure that is made available by Google in my intraweb project.

My difficulty is in the test or not.

I use Delphi 10 Seattle with IntraWe3b 14.0.63.

Thank you in advance for the attention of all my colleagues.

Edited by: Frederick Couto on Feb 1, 2017 1:04 PM

This was tested on IW 14.1.8

Go to the reCAPTCHA site and register your URL:
https://www.google.com/recaptcha/admin#site/337259292

Copy the script in step 1 and paste it into your IW page's extra header.
<script src='https://www.google.com/recaptcha/api.js'></script>
Add the following to HiddenFields of the form:
g-recaptcha-response=

Place a new IWText on the form and set the RawText property to true.
Copy the snippet from step 1 and paste it into the Lines field of the IWText replacing xxxxxxx with the correct key.


Place a new TIdHTTP and TIdSSLIOHandlerSocketOpenSSL on your form.
Point the TIdHTTP's IOHandler to the TIdSSLIOHandlerSocketOpenSSL
(You also might need to setup Proxy etc depending on your server location and copy the required DLLs)

In your OnSumbit event handler: (Cannot be asynchronous event)
//Retrieve the hidden field that was set by the reCAPTCHA widget:
Key := WebApplication.Request.GetContentFieldValue('g-recaptcha-response');
//Submit the key and response to google API: https://developers.google.com/recaptcha/docs/display
lParamList := TStringList.Create;
//Secret Key from Google:
lParamList.Add('secret=xxxxxxxx');//Replace xxxxxxxx with correct key
//Website response in hidden field: g-recaptcha-response
lParamList.Add('response=' + Key);
//Post it and check success:
Result := Pos('"success": true', idHTTP.Post('https://www.google.com/recaptcha/api/siteverify', lParamList)) > 0;
Eitan Arbel

Posts: 508
Registered: 2/24/13
Re: reCaptcha Google  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 27, 2017 7:03 AM   in response to: Johan Erasmus in response to: Johan Erasmus
there is also a component if you have\use CGDevTools
Frederick Couto

Posts: 9
Registered: 12/17/09
Re: reCaptcha Google  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 27, 2017 1:11 PM   in response to: Johan Erasmus in response to: Johan Erasmus
Johan Erasmus wrote:
Frederick Couto wrote:
Hello guys,

I would like information on how to use the reCaptcha procedure that is made available by Google in my intraweb project.

My difficulty is in the test or not.

I use Delphi 10 Seattle with IntraWe3b 14.0.63.

Thank you in advance for the attention of all my colleagues.

Edited by: Frederick Couto on Feb 1, 2017 1:04 PM

This was tested on IW 14.1.8

Go to the reCAPTCHA site and register your URL:
https://www.google.com/recaptcha/admin#site/337259292

Copy the script in step 1 and paste it into your IW page's extra header.
<script src='https://www.google.com/recaptcha/api.js'></script>
Add the following to HiddenFields of the form:
g-recaptcha-response=

Place a new IWText on the form and set the RawText property to true.
Copy the snippet from step 1 and paste it into the Lines field of the IWText replacing xxxxxxx with the correct key.


Place a new TIdHTTP and TIdSSLIOHandlerSocketOpenSSL on your form.
Point the TIdHTTP's IOHandler to the TIdSSLIOHandlerSocketOpenSSL
(You also might need to setup Proxy etc depending on your server location and copy the required DLLs)

In your OnSumbit event handler: (Cannot be asynchronous event)
//Retrieve the hidden field that was set by the reCAPTCHA widget:
Key := WebApplication.Request.GetContentFieldValue('g-recaptcha-response');
//Submit the key and response to google API: https://developers.google.com/recaptcha/docs/display
lParamList := TStringList.Create;
//Secret Key from Google:
lParamList.Add('secret=xxxxxxxx');//Replace xxxxxxxx with correct key
//Website response in hidden field: g-recaptcha-response
lParamList.Add('response=' + Key);
//Post it and check success:
Result := Pos('"success": true', idHTTP.Post('https://www.google.com/recaptcha/api/siteverify', lParamList)) > 0;

Johan Erasmus, thanks for the feedback.

I wonder if you can use a simple example, you can not implement correctly using your step by step.

Thank you!

Edited by: Frederick Couto on Mar 27, 2017 1:11 PM

Johan Erasmus

Posts: 4
Registered: 6/11/06
Re: reCaptcha Google Sample Project  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 27, 2017 11:01 PM   in response to: Frederick Couto in response to: Frederick Couto
Frederick Couto wrote:
Hello guys,

I would like information on how to use the reCaptcha procedure that is made available by Google in my intraweb project.

My difficulty is in the test or not.

I use Delphi 10 Seattle with IntraWe3b 14.0.63.

Thank you in advance for the attention of all my colleagues.

Edited by: Frederick Couto on Feb 1, 2017 1:04 PM

Below is a simple sample project:

reCaptcha.dpr
program reCaptcha;

uses
IWRtlFix,
IWJclStackTrace,
IWJclDebug,
Forms,
IWStart,
Unit1 in 'Unit1.pas' {IWForm1: TIWAppForm},
ServerController in 'ServerController.pas' {IWServerController: TIWServerControllerBase},
UserSessionUnit in 'UserSessionUnit.pas' {IWUserSession: TIWUserSessionBase};

{$R *.res}

begin
TIWStart.Execute(True);
end.

ServerController.dfm
object IWServerController: TIWServerController
OldCreateOrder = False
AppName = 'MyApp'
Description = 'My IntraWeb Application'
DisplayName = 'IntraWeb Application'
Port = 8888
ServerResizeTimeout = 0
ShowLoadingAnimation = True
SessionTimeout = 10
SSLOptions.NonSSLRequest = nsAccept
SSLOptions.Port = 0
SSLOptions.SSLVersions = []
Version = '14.1.8'
AllowMultipleSessionsPerUser = False
OnNewSession = IWServerControllerBaseNewSession
Height = 310
Width = 342
end

ServerController.pas
unit ServerController;

interface

uses
SysUtils, Classes, IWServerControllerBase, IWBaseForm, HTTPApp,
// For OnNewSession Event
UserSessionUnit, IWApplication, IWAppForm, IW.Browser.Browser,
IW.HTTP.Request, IW.HTTP.Reply;

type
TIWServerController = class(TIWServerControllerBase)
procedure IWServerControllerBaseNewSession(ASession: TIWApplication);

private
{ Private declarations }

public
{ Public declarations }
end;

function UserSession: TIWUserSession;
function IWServerController: TIWServerController;

implementation

{$R *.dfm}

uses
IWInit, IWGlobal;

function IWServerController: TIWServerController;
begin
Result := TIWServerController(GServerController);
end;


function UserSession: TIWUserSession;
begin
Result := TIWUserSession(WebApplication.Data);
end;

procedure TIWServerController.IWServerControllerBaseNewSession(
ASession: TIWApplication);
begin
ASession.Data := TIWUserSession.Create(nil, ASession);
end;

initialization
TIWServerController.SetServerControllerClass;

end.


UserSessionUnit.dfm
object IWUserSession: TIWUserSession
OldCreateOrder = False
Height = 150
Width = 215
end

UserSessionUnit.pas
unit UserSessionUnit;

{
This is a DataModule where you can add components or declare fields that are specific to
ONE user. Instead of creating global variables, it is better to use this datamodule. You can then
access the it using UserSession.
}
interface

uses
IWUserSessionBase, SysUtils, Classes;

type
TIWUserSession = class(TIWUserSessionBase)
private
{ Private declarations }
public
{ Public declarations }
end;

implementation

{$R *.dfm}

end.

Unit1.dfm
object IWForm1: TIWForm1
Left = 0
Top = 0
Width = 555
Height = 400
RenderInvisibleControls = True
AllowPageAccess = True
ConnectionMode = cmAny
OnCreate = IWAppFormCreate
Background.Fixed = False
HandleTabs = False
LeftToRight = True
LockUntilLoaded = True
LockOnSubmit = True
ShowHint = True
XPTheme = True
DesignLeft = 8
DesignTop = 8
object IWText1: TIWText
Left = 72
Top = 88
Width = 353
Height = 121
BGColor = clNone
ConvertSpaces = False
Font.Color = clNone
Font.Size = 10
Font.Style = []
FriendlyName = 'IWText1'
Lines.Strings = (
'IWText1')
RawText = False
UseFrame = False
WantReturns = True
end
object IWButton1: TIWButton
Left = 350
Top = 248
Width = 75
Height = 25
Caption = 'Submit'
Color = clBtnFace
Font.Color = clNone
Font.Size = 10
Font.Style = []
FriendlyName = 'IWButton1'
TabOrder = 0
OnClick = IWButton1Click
end
object IWLabel1: TIWLabel
Left = 72
Top = 224
Width = 246
Height = 16
Font.Color = clNone
Font.Size = 10
Font.Style = []
HasTabOrder = False
FriendlyName = 'IWLabel1'
Caption = 'Please verify that you are not a robot...'
end
object IdHTTP1: TIdHTTP
IOHandler = IdSSLIOHandlerSocketOpenSSL1
AllowCookies = True
ProxyParams.BasicAuthentication = False
ProxyParams.ProxyPort = 0
Request.ContentLength = -1
Request.ContentRangeEnd = -1
Request.ContentRangeStart = -1
Request.ContentRangeInstanceLength = -1
Request.Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8'
Request.BasicAuthentication = False
Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
Request.Ranges.Units = 'bytes'
Request.Ranges = <>
HTTPOptions = [hoForceEncodeParams]
Left = 264
Top = 264
end
object IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL
MaxLineAction = maException
Port = 0
DefaultPort = 0
SSLOptions.Method = sslvSSLv23
SSLOptions.SSLVersions = [sslvSSLv2, sslvSSLv3, sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2]
SSLOptions.Mode = sslmUnassigned
SSLOptions.VerifyMode = []
SSLOptions.VerifyDepth = 0
Left = 264
Top = 312
end
end


Unit1.pas
{
1. Go to the reCaptcha URL and register your domain: https://www.google.com/recaptcha/admin
2. Enter your SiteKey and Secret key in the cSiteKey and cSecretKey constants.
3. Run the project and test it.

Notes:
- This is a very simple demo, to improve readability exception handling code has been omitted.
- Only the most basic features of reCAPTCHA has been implimented in this demo.
- Should your web server be behind a proxy server, you might need to setup the proxy settings for IdSSLIOHandlerSocketOpenSSL1
- You cannot use the async event for the button click with this implementation.

Version Information:
- Google reCAPTCHA V2
- Indy 10.6.0.5040
- IntraWeb 14.1.8
}

unit Unit1;

interface

uses
Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes, IWCompButton,
Vcl.Controls, IWVCLBaseControl, IWBaseControl, IWBaseHTMLControl, IWControl,
IWCompText, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL,
IdSSLOpenSSL, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, IWCompLabel;

type
TIWForm1 = class(TIWAppForm)
IWText1: TIWText;
IWButton1: TIWButton;
IdHTTP1: TIdHTTP;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
IWLabel1: TIWLabel;
procedure IWAppFormCreate(Sender: TObject);
procedure IWButton1Click(Sender: TObject);
public
end;

Const
//Replace with the site key you received from the Google reCaptcha site:
cSiteKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
//Replace with the secret key you received from the Google reCaptcha site:
cSecretKey = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy';

implementation

{$R *.dfm}

procedure TIWForm1.IWAppFormCreate(Sender: TObject);
begin
//Add a hidden field for the reCaptcha response:
HiddenFields.Add('g-recaptcha-response=');
//Add the reCaptcha script to header:
ExtraHeader.Add('<script src=''https://www.google.com/recaptcha/api.js''></script>');
//Set the RawText property to true so text will be interpreted as HTML:
IWText1.RawText := True;
//Google Snippet from Step 1 on the Google reCaptcha site:
IWText1.text := '

';
end;


procedure TIWForm1.IWButton1Click(Sender: TObject);
Var
Response : String;
Parameters : TStringList;
Result : Boolean;

begin
//Retrieve the value of the hidden field reCaptcha client response:
Response := WebApplication.Request.GetContentFieldValue('g-recaptcha-response');
//Create a string list to
Parameters := TStringList.Create;
//Secret Key from Google:
Parameters.Add('secret=' + cSecretKey);
//Website response in hidden field: g-recaptcha-response
Parameters.Add('response=' + Response);
//Post it and check success:
Result := Pos('"success": true', IdHTTP1.Post('https://www.google.com/recaptcha/api/siteverify', Parameters)) > 0;
Parameters.Free;
If Result Then
IWLabel1.Caption := 'reCaptcha Success'
Else
IWLabel1.Caption := 'reCaptcha Failed'
end;


initialization
TIWForm1.SetAsMainForm;

end.

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02