android - 身份验证失败 : com. microsoft.identity.client.exception.MsalClientException:缺少类型所需的 token :{0}

标签 android azure-active-directory azure-ad-b2c access-token msal

实现了 Azure B2C 登录。

应用程序突然出现此错误。 找不到问题出在哪里。 谁能帮忙

我们使用以下来源: https://github.com/Azure-Samples/ms-identity-android-java

enter image description here

enter image description here

下面的代码用于生成 KeyHash

public static void getKeyHash(Context mAppContext){
        final String packageName = mAppContext.getPackageName();
        try {
            final PackageInfo info = mAppContext.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
            for (final Signature signature : info.signatures) {
                final MessageDigest messageDigest = MessageDigest.getInstance("SHA");
                messageDigest.update(signature.toByteArray());
                final String signatureHash = Base64.encodeToString(messageDigest.digest(), Base64.NO_WRAP);
                Log.i("Utils", "signatureHash-->"+signatureHash);
            }
        } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
            e.printStackTrace();
            Log.e("Utils", "Unexpected error in verifyRedirectUriWithAppSignature()", e);
        }

    }

具有授予权限的范围和在 android 代码中使用的相同范围 URL

enter image description here

我忘记了 offline_access 权限 添加此权限应用程序工作后

最佳答案

我重现了你的问题并成功解决了。

这是因为您没有在 Azure 门户上对 API 权限进行管理员同意。

请参阅this line在官方示例代码中。

public static List<String> getScopes() {
        return Arrays.asList(
                "https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read");
    }

此示例将范围设置为 https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read 以便它可以获得此 API 的访问 token 。

首先,您应该添加代表您的 Web API 的应用程序注册,并按照 Quickstart: Configure an application to expose a web API 公开 API .

其次,您需要按照Quickstart: Configure a client application to access a web API 在您的客户端应用程序注册中添加API 权限(其客户端ID 在auth_config_b2c.json 文件中配置)。 .

现在最重要的是在 Azure 门户中进行管理员同意:

enter image description here

您可以在 Azure 门户中单击权限进行查看。

enter image description here

将代码中的范围修改为您自己的范围/权限。

public static List<String> getScopes() {
        return Arrays.asList(
                "https://allentest001.onmicrosoft.com/api/demo.read");
    }

配置所有设置后,我们可以在登录后获取访问 token 。

enter image description here

关于android - 身份验证失败 : com. microsoft.identity.client.exception.MsalClientException:缺少类型所需的 token :{0},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67357179/

相关文章:

Android ActionBar 选项长按事件

c# - MSAL.NET 中的 token 缓存序列化不起作用

azure-active-directory - 不需要时需要 Azure AD 管理员同意

azure - B2C Web 应用程序 session 生命周期

azure-active-directory - 如何在 Azure Active Directory B2C 中添加具有本地名称的用户?

azure - 访问 Cosmos DB 文档的授权 token

Android OpenGL ES 颜色字节顺序

java - 组合两个不同的数组列表并根据时间排序

android - 在 onReceive 中处理多个 intent.getStringExtra 的干净方法?

azure - 如何正确授权Azure用户帐户以便它可以创建服务主体?