尝试从 android 应用程序获取 TokenSilentAsync

Trying to acquireTokenSilentAsync from android app

我目前正在使用

implementation 'com.microsoft.identity.client:msal:1.4.0'

为了通过 azure 连接到应用程序,我的 acquireToken() 工作正常并且我得到了用户响应(我将其保存在 SharedPrefs 中)

在那之后(下次登录)我正在尝试像这样执行 acquireTokenSilentAsync :

      SharedPreferences pref = getSharedPreferences();

      Gson gson = new Gson();
      String json = pref.getString("IAccount", "");


      if(!json.isEmpty()){
          Account account = gson.fromJson(json, Account.class);
          AcquireTokenSilentParameters parameters = new AcquireTokenSilentParameters.Builder()
              .withScopes(Arrays.asList(scopes))
              .forAccount(account)
              .fromAuthority(getAuthority())
              .withCallback(getSilentAuthInteractiveCallback())
              .build();
          application.acquireTokenSilentAsync(parameters);
      }

然后进入 onError 回调,除了:

>  E/onError: exception : com.microsoft.identity.client.Account cannot be cast to com.microsoft.identity.client.MultiTenantAccount

知道缺少什么吗?

非常感谢。

此页面可能有帮助:

https://docs.microsoft.com/en-us/azure/active-directory/develop/accounts-overview

特别是,他们不使用 Gson,他们转换帐户以防止您在他们的示例代码中遇到异常。

我会先尝试这样投射:

IMultiTenantAccount multiTenantAccount = (IMultiTenantAccount)account;

并将其用作 forAccount() 中的参数。