【问题标题】:Rendering the Google Recaptcha in Android Studio 3在 Android Studio 3 中呈现 Google Recaptcha
【发布时间】:2018-04-16 19:12:20
【问题描述】:

我正在使用 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 事件处理程序,但它在哪里提到了关于呈现 recaptcha 的内容?

更新 1

当我编写链接中提到的按钮单击代码时...我遇到了一个复杂错误:不可转换类型无法将匿名 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 {
                    }
                }
            });
    }
});

【问题讨论】:

  • 你能把你的代码贴在这里吗?
  • 这与链接中提到的代码相同。我按照您的回答中提到的所有内容进行操作,但面临上述编译错误。 不可转换的类型不能将匿名的 android.view.view.onclicklistener 转换为 java.util.concurrent.executor

标签: recaptcha android-studio-3.0


【解决方案1】:

我使用了下面的代码,现在一切正常。

确保在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 {
                    }
                }
            });
    }
});

【讨论】:

    【解决方案2】:

    根据文章,在您的按钮单击处理程序中,您必须调用方法 SafetyNet.getClient(this).verifyWithRecaptcha(...) 来显示 reCAPTCHA 并处理成功或错误。通过this,您将SDK 句柄提供给您的当前视图,该句柄应在解决reCAPTCHA 后显示。鉴于它是操作系统的一部分,很可能渲染将由 SDK 本身完成。而且很可能它会在一个单独的顶级视图中全屏显示,在解决谜题之前阻止对您的应用程序的访问。

    您应该尝试按照文章中的说明在您的应用中实现它,看看效果如何。然后你可以问一个更具体的问题。

    编辑:您在代码中结合了 2 种技术:从 Google 复制粘贴代码并从中实现匿名类。因此,您在评论中提出的问题是,在第 5 行中使用 (Executor) this 现在不是指您的视图(就像在原始教程中那样),而是指您创建的匿名接口实现 new View.OnClickListener() 的实例。 Ypu 可以参考this answer 看看如何在不干扰已经很复杂的 reCAPTCHA 代码的情况下实现它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-26
      • 2019-06-04
      • 2017-07-03
      • 2015-08-13
      • 2021-10-26
      • 1970-01-01
      • 2017-05-19
      • 2016-12-31
      相关资源
      最近更新 更多