Search code examples
androidtokenhuawei-mobile-serviceshuawei-developershuawei-account

Can the AccessToken Be Obtained by Using the authAccount.getAccessToken() Method After the Silent Login Is Successful


I am integrating Huawei account kit. I want to know whether the AccessToken can be obtained through authAccount by using the following code:

private void silentSignIn() {
        Task<AuthAccount> task = mAuthManager.silentSignIn();
        task.addOnSuccessListener(new OnSuccessListener<AuthAccount>() {
            @Override
            public void onSuccess(AuthAccount authAccount) {

                Log.i(TAG, "silentSignIn success");
                Log.i(TAG, "touxiang: "+authAccount.getAvatarUriString());
            }
        });
        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                //if Failed use getSignInIntent
                if (e instanceof ApiException) {
                    ApiException apiException = (ApiException) e;
                    signIn();
                }
            }
        });
    }

Could anyone give any clue?


Solution

  • You can obtain a user-level AccessToken through silent sign-in.

    1. First you need to check how the mAuthManager object is initialized:

      enter image description here

    2. The mAuthManager object can be initialized only when the following conditions are met:

    //Call the default constructor of HuaweiIdAuthParamsHelper to set authorization parameters.
    AccountAuthParams silentSignInParams = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
            .setAccessToken()
            .createParams();
    //Call the getService method of HuaweiIdAuthManager to initialize the HuaweiIdAuthService object.
    AccountAuthService mAuthManager = AccountAuthManager.getService(getApplicationContext(), silentSignInParams);
    //Call the HuaweiIdAuthService.silentSignIn method to send a silent sign-in request.
    Task<AuthAccount> task = mAuthManager.silentSignIn();
    

    The AccountAuthParams object is required when the mAuthManager object is initialized. When creating this object, you must set the .setAccessToken() method to obtain the user-level access token using the authHuaweiId.getAccessToken() method.

    1. You could check whether you have set the .setAccessToken() method.