MSALs java methods of ISingleAccountPublicClientApplication signIn(activity, loginHint, scopes, authenticationCallback) and acquireTokenSilent(scopes, authority) is deprecated, in favour of build methods.

So I changed the signIn to use SignInParameters.builder() like this:

(below in kotlin)

From:

...signIn(
    signInActivity,
    null, //loginHint 
    arrayOf(accessScope),
    object : AuthenticationCallback { /*omitted*/ },
   )
       

To:

...signIn(
    SignInParameters.builder(
        .withActivity(signInActivity),
        .withScopes(arrayOf(accessScope).toMutableList())
        .withCallback(
            object : AuthenticationCallback { /*omitted*/ 
        })
        .build()
    )


So far, the sign in process seems to go well, no errors, but the problems occures when I call the new acquireTokenSilent method with AcquireTokenSilentParameters used.

From:

val iAuthenticationResult = singleAccountApp.acquireTokenSilent(
                                arrayOf(accessScope),
                                MICROSOFT_ACCESS_AUTHORITY
                        )

To:

val iAuthenticationResult = singleAccountApp.acquireTokenSilent(
                            AcquireTokenSilentParameters(
                                AcquireTokenSilentParameters.Builder()
                                    .withScopes(arrayOf(accessScope).toMutableList())
                                    .fromAuthority(MICROSOFT_ACCESS_AUTHORITY)
                            )
                        )

Now I get following error message in response when acquiring token: The signed in account does not match the provided account which further causes 401 Unauthorized response.

Rewinding back to use the old method just for acquireTokenSilent does not produce any error and the token is provided.

Now I am stuck in new for signIn method, but an old one for the acquireTokenSilent method.

Is there anything I have skipped or forgotten using the newer one ?

1

There are 1 best solutions below

0
On

You should 'provide' the currentAccount of the logged in user.

val parameters = AcquireTokenSilentParameters.Builder()
            .withScopes(arrayOf(accessScope).toMutableList())
            .fromAuthority(MICROSOFT_ACCESS_AUTHORITY)
            .forAccount(singleAccountApp.currentAccount.currentAccount)
            .build()

val iAuthenticationResult = singleAccountApp.acquireTokenSilent(parameters)