我使用 Android SafetyNet ReCaptcha 在我的Android应用中显示Google验证码。问题是,当我对其进行测试时,这些复选框永远不会显示。取而代之的是,验证码很好地显示了出来,并且它的进度条有点动画了,然后完成了而没有任何错误,确认我是人类。 此行为是正常现象,没有任何错误。
但是。我想强制验证码显示上面的复选框,它如上所述工作得很好。“复选框”是指 eg “显示行人过路处的复选框,人类用户必须声称通过检查可以识别”。官方文档没有说明如何执行:https://developer.android.com/training/safetynet/recaptcha#send-request
我遵循了以下文档:https://developer.android.com/training/safetynet/recaptcha#send-request。但是,它没有提供有关如何解决我的问题的任何信息。
我没有发现任何相关问题。实际上,除了一个非常简短的问题(它没有提供任何有用的数据来解决我的问题)之外,我还没有找到有关如何为Android实现ReCaptcha的任何问题。
我将向您展示如何实现他们的Android API ReCaptcha(安全网ReCaptcha)以帮助您。
我的应用程序的用户可以注册,登录和注销。
当用户启动我的应用程序时,将显示一个初始屏幕。如果用户未连接,则邀请他触摸按钮。
2.1。如果他触摸该按钮,则ReCaptcha将启动。
2.1.1. If the ReCaptcha is successfully completed, then the user can sign-up and sign-in with his Google account (I use Google Firebase Auth and even AuthUI).
2.1.2. Otherwise, nothing occurs : he'll have to re-try to complete ReCaptcha.
AppCompatActivity
类):“ onClick”事件处理程序,监听按钮上的“ touch”事件在简历中:我将监听器附加到按钮上。因此,如果单击后者,我将在{synchrone!中自愿呼叫verifyWithRecaptcha
中调用Executor
。然后,我打电话给Google的服务器,以确保验证码是由人而不是机器人完成的,这要归功于我的课程NetworkUseRecaptcha
提供了Google服务器的result
。
final Context that = this;
button_splash_screen_recaptcha.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Executor executor = new Executor() {
@Override
public void execute(@NonNull Runnable command) {
command.run();
}
};
executor.execute(new Runnable() {
@Override
public void run() {
SafetyNet.getClient(that).verifyWithRecaptcha("PUBLIC KEY")
.addOnSuccessListener(executor,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(final SafetyNetApi.RecaptchaTokenResponse response) {
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
String[] parameters = new String[2];
parameters[0] = "SECRET KEY";
parameters[1] = userResponseToken;
new NetworkUseRecaptcha(new RecaptchaPostExecuteCallback() {
@Override
public void onTaskCompleted(String result, boolean background_error) {
if(background_error) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(that,"Error N°2: Unable to check the captcha.", Toast.LENGTH_SHORT).show();
}
});
return;
}
try {
final JSONObject json_response = new JSONObject(result);
if(!json_response.isNull("success") && json_response.getBoolean("success")) {
final List<AuthUI.IdpConfig> providers = ImmutableList.of(
new AuthUI.IdpConfig.GoogleBuilder().build()
);
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.setAlwaysShowSignInMethodScreen(true)
.setLogo(R.drawable.yellow_logo)
.setTheme(R.style.LoginTheme)
.build(),
REQUEST_CODE_SIGN_IN
);
} else {
Toast.makeText(that,"Error N°4: Unable to check the captcha.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(that,"Error N°3: Unable to check the captcha.", Toast.LENGTH_SHORT).show();
}
});
}
}
}).execute(parameters);
}
}
})
.addOnFailureListener(executor, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
System.err.println(e);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(that,"Error N°1: Unable to check the captcha.", Toast.LENGTH_SHORT).show();
}
});
}
});
}
});
}
});
class NetworkUseRecaptcha extends AsyncTask<String, Void, String> {
private final RecaptchaPostExecuteCallback post_execute_callback;
private boolean background_error;
NetworkUseRecaptcha(RecaptchaPostExecuteCallback post_execute_callback) {
this.post_execute_callback = post_execute_callback;
background_error = false;
}
@Override
protected String doInBackground(String[] parameters) {
StringBuilder string_builder = new StringBuilder();
try {
URL url = new URL("https://www.google.com/recaptcha/api/siteverify");
HttpsURLConnection https_url_connection = (HttpsURLConnection) url.openConnection();
https_url_connection.setRequestMethod("POST");
https_url_connection.setDoOutput(false);
https_url_connection.setUseCaches(false);
OutputStream os = https_url_connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
writer.write("secret=" + parameters[0] + "&response=" + parameters[1]);
writer.flush();
writer.close();
os.close();
InputStream input_stream = https_url_connection.getInputStream();
BufferedReader buffered_reader = new BufferedReader(new InputStreamReader(input_stream));
String line;
while((line = buffered_reader.readLine()) != null) {
string_builder.append(line);
}
buffered_reader.close();
} catch (Exception e) {
background_error = true;
}
return string_builder.toString();
}
@Override
protected void onPostExecute(String result) {
post_execute_callback.onTaskCompleted(result, background_error);
}
}
答案 0 :(得分:0)
https://developers.google.com/android/reference/com/google/android/gms/safetynet/SafetyNetClient#verifyWithRecaptcha(java.lang.String):“如果reCAPTCHA确信这是真实设备上的真实用户,它将返回令牌而没有任何挑战。否则,它将提供视觉/听觉挑战,以证明用户的人性返回令牌。”
所以我的愿望不可能具体化...