【问题标题】:MSAL4J - token generation error : Unknown or Invalid instanceMSAL4J - 令牌生成错误:未知或无效实例
【发布时间】:2020-12-11 08:50:24
【问题描述】:

我正在尝试在我的 Java 应用程序中使用 MSAL4j-1.8 jar 生成令牌。 以下是我正在使用的代码:

private static IAuthenticationResult getAccessTokenByClientCredentialGrant() throws Exception {

        ConfidentialClientApplication app = ConfidentialClientApplication.builder(
                clientId,
                ClientCredentialFactory.createFromSecret(secret))
                .authority(authority)
                .build();

        // With client credentials flows the scope is ALWAYS of the shape "resource/.default", as the
        // application permissions need to be set statically (in the portal), and then granted by a tenant administrator
        ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
                Collections.singleton(scope))
                .build();

        CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
        return future.get();
    }

我收到一个错误:

Caused by: com.microsoft.aad.msal4j.MsalServiceException: AADSTS50049: Unknown or invalid instance.
Trace ID: c6a936bf-2b0f-489e-ada3-d2311e708500
Correlation ID: f515dd78-7915-43d7-9020-62631f27c955
Timestamp: 2020-12-10 16:14:09Z
      at com.microsoft.aad.msal4j.AadInstanceDiscoveryProvider.validate(AadInstanceDiscoveryProvider.java:147)
      at com.microsoft.aad.msal4j.AadInstanceDiscoveryProvider.doInstanceDiscoveryAndCache(AadInstanceDiscoveryProvider.java:138)
      at com.microsoft.aad.msal4j.AadInstanceDiscoveryProvider.getMetadataEntry(AadInstanceDiscoveryProvider.java:42)
      at com.microsoft.aad.msal4j.AuthenticationResultSupplier.getAuthorityWithPrefNetworkHost(AuthenticationResultSupplier.java:32)
      at com.microsoft.aad.msal4j.AcquireTokenByAuthorizationGrantSupplier.execute(AcquireTokenByAuthorizationGrantSupplier.java:59)
      at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:59)
      at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:17)
      at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1601)
      at java.lang.Thread.run(Thread.java:818)

知道可能是什么问题吗?

【问题讨论】:

  • 你设置的权限是什么?
  • 怎么样?你的问题解决了吗?

标签: java azure-ad-b2c msal


【解决方案1】:

正如@juunas 所问,您的authority 设置似乎有问题。 试试下面的代码:

import java.util.Collections;
import java.util.concurrent.CompletableFuture;

import com.microsoft.aad.msal4j.ClientCredentialFactory;
import com.microsoft.aad.msal4j.ClientCredentialParameters;
import com.microsoft.aad.msal4j.ConfidentialClientApplication;
import com.microsoft.aad.msal4j.IAuthenticationResult;

public class AADClientCred {
    public static void main(String[] args) {

        String tenantID = "<your tenant ID or name>";
        String clientID = "<your Azure AD app id>";
        String Secret = "<your azure ad app secret>";
        String authority = "https://login.microsoftonline.com/" + tenantID;
        //I use microsoft graph as resource for demo
        String scope = "https://graph.microsoft.com/.default";

        try {
            String access_token = getAccessTokenByClientCredentialGrant(clientID, Secret, authority, scope)
                    .accessToken();

            System.out.println("access token : " + access_token);

        } catch (Exception e) {

            e.printStackTrace();
        }

    }

    private static IAuthenticationResult getAccessTokenByClientCredentialGrant(String clientID, String Secret,
            String authority, String scope) throws Exception {

        ConfidentialClientApplication app = ConfidentialClientApplication
                .builder(clientID, ClientCredentialFactory.createFromSecret(Secret)).authority(authority).build();

        ClientCredentialParameters clientCredentialParam = ClientCredentialParameters
                .builder(Collections.singleton(scope)).build();

        CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
        return future.get();
    }

}

结果:

如果您还有其他问题,请告诉我。

【讨论】:

    猜你喜欢
    • 2017-11-18
    • 2017-12-19
    • 2020-06-19
    • 1970-01-01
    • 2017-11-24
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多