原因:java.net.MalformedURLException:没有使用 ADAL 的 PublicClient 示例的协议

Caused by: java.net.MalformedURLException: no protocol using PublicClient samples for ADAL

我想尝试一下 Microsoft Azure's Active Directory Library for Java. After I pulled the code from github and importing to Eclipse as a maven project, building and executing the PublicClient.java 示例文件,但出现以下异常:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.util.concurrent.ExecutionException: java.net.MalformedURLException: no protocol: 
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:188)
    at src.main.java.PublicClient.getAccessTokenFromUserCredentials(PublicClient.java:36)
    at src.main.java.PublicClient.main(PublicClient.java:23)
Caused by: java.net.MalformedURLException: no protocol: 
    at java.net.URL.<init>(URL.java:585)
    at java.net.URL.<init>(URL.java:482)
    at java.net.URL.<init>(URL.java:431)
    at com.microsoft.aad.adal4j.HttpHelper.openConnection(HttpHelper.java:110)
    at com.microsoft.aad.adal4j.HttpHelper.executeHttpGet(HttpHelper.java:43)
    at com.microsoft.aad.adal4j.HttpHelper.executeHttpGet(HttpHelper.java:38)
    at com.microsoft.aad.adal4j.MexParser.getWsTrustEndpointFromMexEndpoint(MexParser.java:87)
    at com.microsoft.aad.adal4j.AuthenticationContext.processPasswordGrant(AuthenticationContext.java:852)
    at com.microsoft.aad.adal4j.AuthenticationContext.access[=11=](AuthenticationContext.java:839)
    at com.microsoft.aad.adal4j.AuthenticationContext.call(AuthenticationContext.java:129)
    at com.microsoft.aad.adal4j.AuthenticationContext.call(AuthenticationContext.java:1)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

我已经正确指定了我的客户端 ID、用户名和密码。有人可以帮我解决这个问题吗?

我重现了你的问题。问题是样本 PublicClient.java 是一个单独的 Maven 项目,带有 Java.

的 Microsoft AAD 库

解决该问题,需要将示例工程单独导入Eclipse,如下所示。

然后你 运行 安装 maven 以安装示例项目的所有依赖项并执行它。它将无一例外地工作。

感谢 Peter Pan - MSFT,我得以解决此问题。问题是我使用的是 Microsoft 帐户而不是服务帐户。设置服务帐户后,我就可以运行示例了。

要设置服务帐户,请打开 the azure portal,转到所有服务,打开订阅,select 您的订阅,向下滚动并单击 "Roles",然后单击 "Reader"(或您喜欢的任何其他类别,但要确保它具有适当的访问权限)并单击“+”以添加不应是 Microsoft 用户的新用户。