Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

MsalClientException on calling acquireTokenSilentAsync #18

@GanapathiGarre

Description

@GanapathiGarre

Hi,
I am using 'com.microsoft.identity.client:msal:0.2.+' in my Android application with B2C configuration.
It is working as expected when I was trying to login using com.microsoft.identity.client.BrowserTabActivity with my credentials and I am also getting my accessToken successfully to use for my server calls.
Before making any server call I am calling acquireTokenSilentAsync method to make sure the existing access token is not expired and also to get new token in case of its expiry. The method is returning same token in its call back response AuthenticationResult as long as it is not in expired.
But the problem here is, once the token got expired I am not receiving a new token. Instead receiving an exception saying
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

Any help would be appreciated. Thank you.

For more info the below is the Kotlin code that I am using to get new token silently

fun getMSALAuthTokenSilently(){
var accounts = App.loginClientAppContext?.accounts

accounts?.let {
        
        App.loginClientAppContext?.acquireTokenSilentAsync(arrayOf(App.appContext?.resources?.getString(R.string.redirect_uri_scope)), it[0], object : AuthenticationCallback{
            override fun onSuccess(authenticationResult: AuthenticationResult?) {
                //Storing ACCESS_TOKEN into preferences
                authenticationResult?.accessToken?.let { token ->
                    if (token.isNotEmpty()){
                        Log.d("MSALSilent token succ:", token )
                    }else{
                        Log.d("MSALSilent token error:", "token is empty" )
                    }
                }
            }

            override fun onCancel() {
                Log.d("MSALSilent token error:", "cancelled" )
            }

            override fun onError(exception: MsalException?) {
                when (exception) {
                    is MsalClientException -> {
                        /* Exception inside MSAL, more info inside MsalError.java */
                        Log.d("MSALSilent token error:", "MsalClientException "+exception?.message )
                    }
                    is MsalServiceException -> {
                        /* Exception when communicating with the STS, likely config issue */
                        Log.d("MSALSilent token error:", "MsalServiceException "+exception?.message )
                    }
                    is MsalUiRequiredException -> {
                        /* Tokens expired or no session, retry with interactive */
                        Log.d("MSALSilent token error:", "MsalUiRequiredException "+exception?.message )
                    }
                }
            }
        })
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions