尝试从安卓应用程序获取 acquireTokenSilentAsync

huangapple go评论96阅读模式
英文:

Trying to acquireTokenSilentAsync from android app

问题

我目前正在使用以下代码连接到 Azure 应用:

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

通过 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

有什么想法,是什么导致了这个问题?

非常感谢。

英文:

I'm currently using

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

to connect to the app via azure , my acquireToken() works fine and I get the user in response ( I save it in the SharedPrefs)

After that (next login) I'm trying to do acquireTokenSilentAsync like so :

      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);
      }

And get to the onError callback with the exception of :

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

Any ideas what's missing ?

Much Appreciated.

答案1

得分: 1

这个页面可能会有帮助:

<https://learn.microsoft.com/en-us/azure/active-directory/develop/accounts-overview>

特别是,他们不使用 Gson,并且他们进行了类型转换以防止你在他们的示例代码中遇到的异常。

我建议首先尝试像这样进行类型转换:

IMultiTenantAccount multiTenantAccount = (IMultiTenantAccount)account;

然后将其用作 forAccount() 方法的参数。

英文:

This page may help:

<https://learn.microsoft.com/en-us/azure/active-directory/develop/accounts-overview>

In particular, they don't use a Gson and they cast the account to prevent the exception you get in their example code.

I would start by trying to cast like this:

IMultiTenantAccount multiTenantAccount = (IMultiTenantAccount)account;

and using that as the argument in forAccount().

huangapple
  • 本文由 发表于 2020年7月30日 19:48:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63172531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定