Android Question How use recaptcha google in app

Status
Not open for further replies.

Pooya1

Active Member
Licensed User
Hi
I could use Google's recaptcha in my website
Now i need use it in app
I searched about it in android
I have to use safetynet in b4a
Please help me
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Sign for this service: https://developer.android.com/training/safetynet/recaptcha
You need to get a site key and secret key.
The site key is used in the Android app and the secret key is used on the server.

2. Add:
B4X:
#AdditionalJar: com.google.android.gms:play-services-safetynet
3. Add in manifest editor:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
4. Call verify in your activity:
B4X:
'RecaptchaKey is a process global string.
Sub Verify
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim Client As JavaObject
   Client = Client.InitializeStatic("com/google/android/gms/safetynet.SafetyNet".Replace("/", ".")).RunMethod("getClient", Array(ctxt))
   Dim task As JavaObject = Client.RunMethod("verifyWithRecaptcha", Array(RecaptchaKey))
   Dim start As Long = DateTime.Now
   Do While task.RunMethod("isComplete", Null) = False And start + 10000 > DateTime.Now
       Sleep(100)
   Loop
   If task.RunMethod("isSuccessful", Null) Then
       Dim token As JavaObject = task.RunMethod("getResult", Null)
       Dim tokenkey As String = token.RunMethod("getTokenResult", Null)
       Log(tokenkey)
       VerifyToken(tokenkey)  '<---the token should be sent to your server and the server should verify the token
   Else
       Log("error")
   End If
End Sub

5. Verify token on the server:
B4X:
'ServerSecretKey is a process global string.
Sub VerifyToken(token As String)
   'verify the token
   Dim j As HttpJob
   j.Initialize("", Me)
   j.PostString("https://www.google.com/recaptcha/api/siteverify", $"secret=${ServerSecretKey}&response=${token}"$)
   Wait For (j) JobDone (j As HttpJob)
   If j.Success Then
       Dim parser As JSONParser
       parser.Initialize(j.GetString)
       Dim m As Map = parser.NextObject
       Dim success As Boolean = m.Get("success")
       If success Then
           Log("User verified!!!!")
       Else
           Log("error!!!")
       End If
   End If
   j.Release
End Sub

Note that this code will only work in portrait mode. It will require some changes to work in landscape.

The server step can technically be done from the mobile app, however it is less secure.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Screenshot:

SS-2018-11-01_09.50.23.png
 
Upvote 0

Pooya1

Active Member
Licensed User
Sorry for open this post
I use your sample but i have error Generating R file. Error
I updated SDK completely
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
B4X:
'RecaptchaKey is a process global string.
Sub Verify
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim Client As JavaObject
   Client = Client.InitializeStatic("com/google/android/gms/safetynet.SafetyNet".Replace("/", ".")).RunMethod("getClient", Array(ctxt))
   Dim task As JavaObject = Client.RunMethod("verifyWithRecaptcha", Array(RecaptchaKey))
   Dim start As Long = DateTime.Now
   Do While task.RunMethod("isComplete", Null) = False And start + 10000 > DateTime.Now
       Sleep(100)
   Loop
   If task.RunMethod("isSuccessful", Null) Then
       Dim token As JavaObject = task.RunMethod("getResult", Null)
       Dim tokenkey As String = token.RunMethod("getTokenResult", Null)
       Log(tokenkey)
       VerifyToken(tokenkey)  '<---the token should be sent to your server and the server should verify the token
   Else
       Log("error")
   End If
End Sub


I got "error"; that is meaning task.RunMethod("isSuccessful", Null) is false!!! Why?
I used mine RecaptchaKey
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Oh
Sorry
Solved
The problem is by mistake i wrote website name in package name field in google recaptcha registration

Sorry @Erel for taking your time
 
Upvote 0

alimanam3386

Active Member
Licensed User
Longtime User
4. Call verify in your activity:
B4X:
'RecaptchaKey is a process global string.
Sub Verify
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim Client As JavaObject
   Client = Client.InitializeStatic("com/google/android/gms/safetynet.SafetyNet".Replace("/", ".")).RunMethod("getClient", Array(ctxt))
   Dim task As JavaObject = Client.RunMethod("verifyWithRecaptcha", Array(RecaptchaKey))
   Dim start As Long = DateTime.Now
   Do While task.RunMethod("isComplete", Null) = False And start + 10000 > DateTime.Now
       Sleep(100)
   Loop
   If task.RunMethod("isSuccessful", Null) Then
       Dim token As JavaObject = task.RunMethod("getResult", Null)
       Dim tokenkey As String = token.RunMethod("getTokenResult", Null)
       Log(tokenkey)
       VerifyToken(tokenkey)  '<---the token should be sent to your server and the server should verify the token
   Else
       Log("error")
   End If
End Sub

in server side ( java script ) I got this error after verification of token :
JSON:
"error": {
        "success": false,
        "error-codes": [
            "timeout-or-duplicate"
        ]
    }

so , How can I rest the token ( get new token ) like use in a browser ( refresh page) ?
 
Last edited:
Upvote 0
Status
Not open for further replies.
Top