我已经尝试了一周在 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 对象但它的“值”属性为空(而我之前确实将我的公共证书放入其中)......这是否意味着它真的为空还是出于安全原因只是一个空的财产?