3

我已经尝试了一周在 Azure Active Directory 上使用提供客户端凭据授权的 Java 应用程序进行身份验证,以便检索访问令牌以针对 Outlook Office365 REST API,但未能成功。服务器总是返回一个错误:

com.microsoft.aad.adal4j.AuthenticationException: {"error":"unauthorized_client","error_description":"AADSTS70002: Error validating credentials. AADSTS50064: Credential validation failed

我已经关注了 MSDN 博客文章http://blogs.msdn.com/b/exchangedev/archive/2015/01/21/building-demon-or-service-apps-with-office-365-mail-calendar-and -contacts-apis-oauth2-client-credential-flow.aspx并将我的公共证书上传到 Azure 上的应用程序清单中。

我在 0.0.4 版中使用 Java 库 ADAL4J,代码如下:

        service = Executors.newFixedThreadPool(1);

        final KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE");
        keystore.load(new FileInputStream(MyApp.class.getResource(TestConfiguration.AAD_CERTIFICATE_PATH).getFile()),
                        TestConfiguration.AAD_CERTIFICATE_PASSWORD.toCharArray());
        final String alias = keystore.aliases().nextElement();
        final PrivateKey key = (PrivateKey) keystore.getKey(alias, TestConfiguration.AAD_CERTIFICATE_PASSWORD.toCharArray());
        final X509Certificate cert = (X509Certificate) keystore.getCertificate(alias);
        final AsymmetricKeyCredential asymmetricKeyCredential = AsymmetricKeyCredential.create(TestConfiguration.AAD_CLIENT_ID, key, cert);
        ctx = new AuthenticationContext(TestConfiguration.AAD_TENANT_ENDPOINT, false, service);
        final Future<AuthenticationResult> result = ctx.acquireToken(TestConfiguration.AAD_RESOURCE_ID, asymmetricKeyCredential,null);
        ar = result.get();

这是我的 TestConfiguration 对象:

public final class TestConfiguration {

public final static String AAD_HOST_NAME = "login.windows.net";
public final static String AAD_TENANT_NAME = "MYTENANT.onmicrosoft.com"; 
public final static String AAD_TENANT_ENDPOINT = "https://" + AAD_HOST_NAME + "/" + AAD_TENANT_NAME + "/";
public final static String AAD_CLIENT_ID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
public final static String AAD_CLIENT_SECRET = "xxxxxxxxxx";
public final static String AAD_RESOURCE_ID = "https://outlook.office365.com/"; 
public final static String AAD_CERTIFICATE_PATH = "/MyCert.pfx";
public final static String AAD_CERTIFICATE_PASSWORD = "xxxxxxx";
}

这是产生的 com.microsoft.aad.adal4j.AdalOAuthRequest 请求对象属性:

authorization : null
contentType : application/x-www-form-urlencoded; charset=UTF-8
extraHeaderParams : {client-request-id=d942e921-71d7-47ef-9f97-29e8bb4122aa, x-client-OS=Windows 7, x-client-SKU=java, return-client-request-id=true, x-client-CPU=amd64, x-client-VER=1.0}
method : POST
query : client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer&grant_type=client_credentials&client_assertion=eyJhbGciOiJSUzI1NiIsIn ... cut ... G0U4MeBV69EGauQ&resource=https%3A%2F%2Foutlook.office365.com%2F
url : https://login.windows.net/mytenant.onmicrosoft.com/oauth2/token

我可能在哪里错过了什么?

谢谢你的帮助。

PS:当我从 Azure 下载我的应用程序清单时,我发现我的“keyCredentials”JSON 对象但它的“值”属性为空(而我之前确实将我的公共证书放入其中)......这是否意味着它真的为空还是出于安全原因只是一个空的财产?

4

1 回答 1

0

commons-codec 版本需要从 1.4 更新到 1.5 以使编码正常工作。http://commons.apache.org/proper/commons-codec/changes-report.html#a1.5

来自埃里克的编辑

我终于设法通过将我的 common-codecs 工件从 1.4 (它不工作)升级到 1.5 来检索访问令牌。

我们开始考虑通用编解码器工件,因为提供的 JWT 令牌有一个公共证书值,当我的应用程序生成时插入了“\r\n”,而当它是从专用示例测试应用程序生成时却没有。

通过从 com.microsoft.aad.adal4j.AsymmetricKeyCredential 调用 org.apache.commons.codec.binary.Base64.encodeBase64String(byte[]) 将这些“\r\n”字符插入到“x5c”JWT 标头属性中.getPublicCertificate()。

看来我的应用程序已经依赖于不适合 adal4j 的 common-codecs v1.4。

于 2015-02-25T20:58:01.467 回答