iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 29
0
Mobile Development

Andriod Studio 菜鳥的學習分享系列 第 29

[Android Studio菜鳥的學習分享]我不是機器人-Google reCAPTCHA

Google reCAPTCHA是Google開發的防堵機器人驗證API,
原本是設計給網頁使用,
後來發現它也可以在APP上使用,
這樣在一些例如申請帳號的流程上就能加上它,
防止一些機器人來辦帳號唷~


結果預覽:

https://i.imgur.com/Xt4zM63.gif


Gradle Scripts

加入play-services-safetynet套件

implementation 'com.google.android.gms:play-services-safetynet:17.0.0'

註冊Google reCAPTCHA金鑰

Step01:

開啟網站
Google reCAPTCHA官方網站

Step02:

(1)標籤:自訂你的專案名稱
(2)類型:請選擇V2的reCAPTCHA Android
(3)套件:我填的是build.gradle內的applicationId
https://ithelp.ithome.com.tw/upload/images/20201011/20129524hAT4QWkSjE.jpg

https://ithelp.ithome.com.tw/upload/images/20201011/20129524uWCNudTOOt.jpg

Step03-複製你的金鑰:

https://ithelp.ithome.com.tw/upload/images/20201011/20129524258f2AOpXU.jpg


activity_main.xml

https://ithelp.ithome.com.tw/upload/images/20201011/20129524Do6uOm3smS.jpg

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我不是機器人"
        android:textSize="20dp"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:id="@+id/btn_robot">

    </Button>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="尚未驗證"
        android:textSize="25dp"
        android:textAlignment="center"
        android:textColor="@color/design_default_color_error"
        android:layout_marginTop="20dp"
        android:id="@+id/text_robot">

    </TextView>

</LinearLayout>

MainActivity.java

Step01:

實作GoogleApiClient.ConnectionCallbacks和GoogleApiClient.OnConnectionFailedListener
對著紅紅的它們alt+enter實作出:
(1)onConnected - 連線成功
(2)onConnectionSuspended - 連線中斷
(3)onConnectionFailed - 連線失敗

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Toast.makeText(this, "Google Services 連線成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this, "Google Services 連線中斷,連線中斷代號 :" + i, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Toast.makeText(this, "Google Services 連線失敗,失敗原因 :" + connectionResult.getErrorMessage(), Toast.LENGTH_SHORT).show();
    }

}

Step02:

    private Button btn_robot;
    private TextView text_robot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text_robot = findViewById(R.id.text_robot);
        btn_robot = findViewById(R.id.btn_robot);

        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(SafetyNet.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        googleApiClient.connect();

        btn_robot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setText(0);
                SafetyNet.SafetyNetApi.verifyWithRecaptcha(googleApiClient,"你的金鑰").setResultCallback(new ResultCallback<SafetyNetApi.RecaptchaTokenResult>() {
                    @Override
                    public void onResult(@NonNull SafetyNetApi.RecaptchaTokenResult recaptchaTokenResult) {
                        Status status = recaptchaTokenResult.getStatus();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if((status != null) && status.isSuccess()){
                                    //驗證通過
                                    setText(1);
                                }
                                else {
                                    //驗證失敗
                                    setText(0);
                                }
                            }
                        });
                    }
                });
            }
        });
    }

    private void setText(int i) {
        if (i == 1){
            text_robot.setText("驗證通過");
            text_robot.setTextColor(Color.GREEN);
        }
        else{
            text_robot.setText("驗證失敗");
            text_robot.setTextColor(Color.RED);
        }

    }

上一篇
[Android Studio菜鳥的學習分享]SQLite資料庫查詢工具-stetho
下一篇
[Android Studio菜鳥的學習分享]完賽結語
系列文
Andriod Studio 菜鳥的學習分享30

尚未有邦友留言

立即登入留言