Android client demo

更新时间:
复制 MD 格式

This topic describes how to run the fusion authentication demo and provides sample code to help you get started quickly. For detailed instructions on integrating the Android client SDK, see Integrate the fusion authentication SDK for Android.

Step 1: Download and extract the SDK

Log on to the Phone Number Verification Service console. On the Overview page, find the API & SDK section on the right and click Download Now. Follow the on-screen instructions to download the required SDK.

Step 2: Configure authentication scheme and policy

Create authentication scheme

Log in to the Phone Number Verification Service console and create a fusion authentication scheme. Then, find and copy the scheme code from the scheme list.

Bind authentication policy

Log in to the Phone Number Verification Service console. Navigate to Fusion Authentication Service > Authentication Policy Settings > Scheme Policy to associate the scheme with a scene.

Step 3: Open the demo project

In Android Studio, select Open an Existing Project and navigate to the demo folder. Please wait while the project loads its dependencies.

Important

This topic uses Android Studio 4.0 for demonstration. If you use a different IDE version, you might encounter build errors. If so, modify the Gradle version as prompted. For more information on tools and environment setup, see Client SDK reference.

Step 4: Select an access mode

To help you run the demo before you fully configure your server, the demo includes a quick access mode. You can switch between access modes by setting the value of TOKEN_MODEL in com.alicom.fusion.demo.Constant.

  • TOKEN_MODEL: Set to 1 for quick access mode.

  • TOKEN_MODEL: Set to 2 for normal access mode (default).

Normal access mode

Normal access mode requires integrating both the fusion authentication server and client SDKs. This mode allows for full debugging and access to all features.

First, follow the Development guide to integrate your fusion authentication app server. Use the app server to obtain an authentication token and verify the final authentication result.

  1. Set the following parameters in the com.alicom.fusion.demo.Constant class.

    • SCHEME_CODE: Enter the scheme code that you created in the console.

    • NETURL: Enter your app server address.

    • GETAUTHREQUESTACTION: Enter the name of your API for obtaining an authentication token.

    • VERIFYREQUESTACTION: Enter the name of your API for token verification.

  2. Modify the relevant parameters in the com.alicom.fusion.demo.net.HttpRequestUtil class.

    This method retrieves an authentication token. You can replace the parameters in this method with those for your own server API. This network request uses HttpURLConnection. You can change it to HttpsURLConnection based on your server environment.

    public static GetAuthTokenResult getAuthToken(Context context) {
        StringBuilder builder = new StringBuilder();
        builder.append("Action=");
        builder.append(Constant.GETAUTHREQUESTACTION);
        builder.append("&Platform=Android");
        builder.append("&PackageName=");
        builder.append(PhoneUtils.getPackageName(context));
        builder.append("&SchemeCode=");
        builder.append(Constant.SCHEME_CODE);
        // Token validity period in seconds
        builder.append("&DurationSeconds=");
        builder.append(3600);
        builder.append("&PackageSign=");
        builder.append(PackageUtil.getSign(context));
        String result = getHttp(Constant.NETURL, builder.toString());
        Log.d(TAG, "getAuthToken result is " + result);
        GetAuthTokenResult authTokenResult = ResultUtils.getAuthTokenResult(result);
        return authTokenResult;
    }
    This method exchanges a token for a phone number. You can replace the parameters in this method with those for your own server API. This network request uses HttpURLConnection. You can change it to HttpsURLConnection based on your server environment.
    public static VerifyTokenResult verifyToken(Context context, String token) {
        StringBuilder builder = new StringBuilder();
        builder.append("platform=Android&");
        builder.append("Action=");
        builder.append(Constant.VERIFYREQUESTACTION);
        builder.append("&SchemeCode=");
        builder.append(Constant.SCHEME_CODE);
        builder.append("&VerifyToken=");
        builder.append(token);
        String result = postHttp(Constant.NETURL, builder.toString());
        Log.d(TAG, "verifyToken result is " + result);
        VerifyTokenResult verifyTokenResult = ResultUtils.getVerifyTokenResult(result);
        return verifyTokenResult;
    }

Quick access mode

Quick access mode lets you debug the demo and test basic features without integrating the server-side API.

  1. In OpenAPI Explorer, enter the API parameters. For more information on the parameters, see Get an authentication token for fusion authentication.

    Note
    • SchemeCode: Enter the scheme code that you created in the console.

    • PackageName: In the app/build.gradle file, find the package name in the android.defaultConfig.applicationId property.

    • PackageSign: Package signature. You can download and install the Android App Signature Tool on a mobile device to obtain the package signature of your Android app. This signature is required for authentication.

  2. Click Send Request.

  3. On the Call Result page, obtain the temporary authentication token. If the call is successful, the API returns a status code of 200. In the JSON response body, find the Token field and copy its value.

  4. In the com.alicom.fusion.demo.Constant class, configure the following settings:

    • LOCAL_TOKEN: Enter the temporary authentication token that you obtained.

    • SCHEME_CODE: Enter the scheme code that you created.

    • TOKEN_MODEL: Enter 1.

  5. After the demo finishes running, obtain the phone number verification token from the onVerifySuccess method of the AlicomFusionAuthCallBack callback. You can then use this token to perform verification in the console. For more information, see server-side integration.

    @Override
    public void onVerifySuccess(String token, String s1, AlicomFusionEvent alicomFusionEvent) {
        Log.d(TAG, "AlicomFusionAuthCallBack---onVerifySuccess  " + token);
        new Thread(new Runnable() {
            @Override
            public void run() {
                VerifyTokenResult verifyTokenResult = TokenActionFactory.verifyToken(GlobalInfoManager.getInstance().getContext(), token);
                updateBusiness(verifyTokenResult, s1);
            }
        }).start();
    }

Step 5: Build and run the project

  1. Connect an Android phone to a computer, and enable Developer mode and USB debugging mode.

  2. On the IDE toolbar, click the image..png icon to build and run the project.

  3. Try the features.

    Note

    Ensure that mobile data is enabled on your device. The service supports 3G networks from China Unicom and China Mobile, but using them may increase API response times.

Sample code

Initialize SDK

mAlicomFusionBusiness = new AlicomFusionBusiness();

Provide token

AlicomFusionAuthToken token=new AlicomFusionAuthToken();
        token.setAuthToken(GlobalInfoManager.getInstance().getToken());
        mAlicomFusionBusiness.initWithToken(GlobalInfoManager.getInstance().getContext(), Constant.SCHEME_CODE,token);

Set AlicomFusionAuthCallBack

mAlicomFusionAuthCallBack = new AlicomFusionAuthCallBack() {
            @Override
            public AlicomFusionAuthToken onSDKTokenUpdate() {
                Log.d(TAG, "AlicomFusionAuthCallBack---onSDKTokenUpdate");
                AlicomFusionAuthToken token=new AlicomFusionAuthToken();
                CountDownLatch latch=new CountDownLatch(1);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        GlobalInfoManager.getInstance().setToken(HttpRequestUtil.getAuthToken(GlobalInfoManager.getInstance().getContext()));
                        latch.countDown();
                    }
                }).start();
                try {
                    latch.await();
                    token.setAuthToken(GlobalInfoManager.getInstance().getToken());
                } catch (InterruptedException e) {
                }
                return token;
            }

            @Override
            public void onSDKTokenAuthSuccess() {
                Log.d(TAG, "AlicomFusionAuthCallBack---onSDKTokenAuthSuccess");
                verifySuccess=true;
            }

            @Override
            public void onSDKTokenAuthFailure(AlicomFusionAuthToken token, AlicomFusionEvent alicomFusionEvent) {
                Log.d(TAG, "AlicomFusionAuthCallBack---onSDKTokenAuthFailure "+alicomFusionEvent.toString());
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Looper.prepare();
                        String token = HttpRequestUtil.getAuthToken(GlobalInfoManager.getInstance().getContext());
                        GlobalInfoManager.getInstance().setToken(token);
                        AlicomFusionAuthToken authToken=new AlicomFusionAuthToken();
                        authToken.setAuthToken(GlobalInfoManager.getInstance().getToken());
                        mAlicomFusionBusiness.updateToken(authToken);
                    }
                }).start();
            }

            @Override
            public void onVerifySuccess(String token, String s1, AlicomFusionEvent alicomFusionEvent) {
                Log.d(TAG, "AlicomFusionAuthCallBack---onVerifySuccess  " +token);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String mobilNum = HttpRequestUtil.verifyToken(GlobalInfoManager.getInstance().getContext(), token);
                        updateBusiness(mobilNum,s1);
                    }
                }).start();
            }

            @Override
            public void onHalfWayVerifySuccess(String nodeName, String maskToken, AlicomFusionEvent alicomFusionEvent, HalfWayVerifyResult halfWayVerifyResult) {
                Log.d(TAG, "AlicomFusionAuthCallBack---onHalfWayVerifySuccess  "+maskToken);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String result = HttpRequestUtil.verifyToken(GlobalInfoManager.getInstance().getContext(), maskToken);
                        updateBusinessHalfWay(result,halfWayVerifyResult,nodeName);
                    }
                }).start();
            }

            @Override
            public void onVerifyFailed(AlicomFusionEvent alicomFusionEvent, String s) {
                Log.d(TAG, "AlicomFusionAuthCallBack---onVerifyFailed "+alicomFusionEvent.toString()());
                mAlicomFusionBusiness.continueSceneWithTemplateId("100001",false);
            }

            @Override
            public void onTemplateFinish(AlicomFusionEvent alicomFusionEvent) {
                Log.d(TAG, "AlicomFusionAuthCallBack---onTemplateFinish  "+alicomFusionEvent.toString());
                sum=0;
                mAlicomFusionBusiness.stopSceneWithTemplateId("100001");
            }

            @Override
            public void onAuthEvent(AlicomFusionEvent alicomFusionEvent) {
                Log.d(TAG, "AlicomFusionAuthCallBack---onAuthEvent"+alicomFusionEvent.toString());
            }

            @Override
            public String onGetPhoneNumberForVerification(String s, AlicomFusionEvent alicomFusionEvent) {
                Log.d(TAG, "AlicomFusionAuthCallBack---onGetPhoneNumberForVerification");
                return GlobalInfoManager.getInstance().getUserInfo();
            }

            @Override
            public void onVerifyInterrupt(AlicomFusionEvent alicomFusionEvent) {
                Log.d(TAG, "AlicomFusionAuthCallBack---onVerifyInterrupt"+alicomFusionEvent.toString());
            }
        };
        mAlicomFusionBusiness.setAlicomFusionAuthCallBack(mAlicomFusionAuthCallBack);

Start scene

mAlicomFusionBusiness.startSceneWithTemplateId(PersonalCenterFragment.this.getActivity(), "100001");

Continue scene

mAlicomFusionBusiness.continueSceneWithTemplateId("100001",false);

Stop scene

mAlicomFusionBusiness.stopSceneWithTemplateId("100001");

Destroy SDK

mAlicomFusionBusiness.destory();