在 Android Studio 3 中渲染 Google Recaptcha
Rendering the Google Recaptcha in Android Studio 3
我正在使用 Android Studio 3
I am following this article to learn how to use Google Recaptcha in Android Studio.
使用这个安装包:implementation 'com.google.android.gms:play-services-safetynet:12.0.1'
API 密钥也已注册。
我看到有 onClick 事件处理程序,但它在哪里提到渲染重新验证码?
更新 1
当我编写 link 中提到的按钮点击代码时...我遇到了一个复杂的错误:不可转换的类型无法将匿名 android.view.view.onclicklistener 转换为 java.util.concurrent.executor
评论中要求的代码
btn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SafetyNet.getClient(this).verifyWithRecaptcha("")
.addOnSuccessListener((Executor) this,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
// Indicates communication with reCAPTCHA service was
// successful.
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
// Validate the user response token using the
// reCAPTCHA siteverify API.
}
}
})
.addOnFailureListener((Executor) this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
// An error occurred when communicating with the
// reCAPTCHA service. Refer to the status code to
// handle the error appropriately.
ApiException apiException = (ApiException) e;
int statusCode = apiException.getStatusCode();
} else {
}
}
});
}
});
根据这篇文章,在您的按钮点击处理程序中,您必须调用方法 SafetyNet.getClient(this).verifyWithRecaptcha(...)
来显示 reCAPTCHA 并处理成功或错误。传递 this
,您将 SDK 句柄提供给当前视图,该视图应在解决 reCAPTCHA 后显示。渲染很可能会由 SDK 本身完成,因为它是 OS 的一部分。在解开谜语之前,它很可能会在单独的顶级视图中全屏显示以阻止对您的应用程序的访问。
您应该尝试按照文章中的描述在您的应用中实现它,看看效果如何。然后你可以问一个更具体的问题。
编辑: 您在代码中结合了两种技术:从 Google 复制粘贴代码并从中实现匿名 class。所以你在评论中提出的问题是,在第 5 行中使用 (Executor) this
现在不是指你的视图(因为它在原始教程中),而是指匿名接口实现的实例 new View.OnClickListener()
你创造了。 Ypu 可以参考 this answer 以了解如何在不干扰已经复杂的 reCAPTCHA 代码的情况下实现它。
我使用了下面的代码,现在一切正常。
确保在 activity
中实现 Executor
btn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SafetyNet.getClient(Activity.this).verifyWithRecaptcha("")
.addOnSuccessListener((Activity) MyActivity.this,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
// Indicates communication with reCAPTCHA service was
// successful.
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
// Validate the user response token using the
// reCAPTCHA siteverify API.
}
}
})
.addOnFailureListener((Activity) MyActivity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
// An error occurred when communicating with the
// reCAPTCHA service. Refer to the status code to
// handle the error appropriately.
ApiException apiException = (ApiException) e;
int statusCode = apiException.getStatusCode();
} else {
}
}
});
}
});
我正在使用 Android Studio 3
I am following this article to learn how to use Google Recaptcha in Android Studio.
使用这个安装包:implementation 'com.google.android.gms:play-services-safetynet:12.0.1'
API 密钥也已注册。
我看到有 onClick 事件处理程序,但它在哪里提到渲染重新验证码?
更新 1
当我编写 link 中提到的按钮点击代码时...我遇到了一个复杂的错误:不可转换的类型无法将匿名 android.view.view.onclicklistener 转换为 java.util.concurrent.executor
评论中要求的代码
btn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SafetyNet.getClient(this).verifyWithRecaptcha("")
.addOnSuccessListener((Executor) this,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
// Indicates communication with reCAPTCHA service was
// successful.
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
// Validate the user response token using the
// reCAPTCHA siteverify API.
}
}
})
.addOnFailureListener((Executor) this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
// An error occurred when communicating with the
// reCAPTCHA service. Refer to the status code to
// handle the error appropriately.
ApiException apiException = (ApiException) e;
int statusCode = apiException.getStatusCode();
} else {
}
}
});
}
});
根据这篇文章,在您的按钮点击处理程序中,您必须调用方法 SafetyNet.getClient(this).verifyWithRecaptcha(...)
来显示 reCAPTCHA 并处理成功或错误。传递 this
,您将 SDK 句柄提供给当前视图,该视图应在解决 reCAPTCHA 后显示。渲染很可能会由 SDK 本身完成,因为它是 OS 的一部分。在解开谜语之前,它很可能会在单独的顶级视图中全屏显示以阻止对您的应用程序的访问。
您应该尝试按照文章中的描述在您的应用中实现它,看看效果如何。然后你可以问一个更具体的问题。
编辑: 您在代码中结合了两种技术:从 Google 复制粘贴代码并从中实现匿名 class。所以你在评论中提出的问题是,在第 5 行中使用 (Executor) this
现在不是指你的视图(因为它在原始教程中),而是指匿名接口实现的实例 new View.OnClickListener()
你创造了。 Ypu 可以参考 this answer 以了解如何在不干扰已经复杂的 reCAPTCHA 代码的情况下实现它。
我使用了下面的代码,现在一切正常。
确保在 activity
中实现 Executorbtn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SafetyNet.getClient(Activity.this).verifyWithRecaptcha("")
.addOnSuccessListener((Activity) MyActivity.this,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
// Indicates communication with reCAPTCHA service was
// successful.
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
// Validate the user response token using the
// reCAPTCHA siteverify API.
}
}
})
.addOnFailureListener((Activity) MyActivity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
// An error occurred when communicating with the
// reCAPTCHA service. Refer to the status code to
// handle the error appropriately.
ApiException apiException = (ApiException) e;
int statusCode = apiException.getStatusCode();
} else {
}
}
});
}
});