我按照 Power BI Web 应用身份验证文档中提供的说明进行操作,并且能够获取身份验证 token 。但该过程要求用户在 GUI 中输入凭据来获取身份验证码。
相反,我想进行静默身份验证,即没有 GUI 身份验证。我尝试了几种方法,例如使用 HHTPClient 和下面链接中的引用代码
https://gist.github.com/dquig/a4f2f02fe3e306cebe2e
但它要求 client_secret ,不幸的是没有方法接受 cleint_secret 以及 Resourceuri、clientId、用户名、密码、回调。 am 使用1.0.0版本的azure adal4j。难道是版本问题?任何帮助将不胜感激
最佳答案
由于使用客户端应用程序获取Azure AD访问 token ,因此需要在GUI身份验证中输入用户名和密码。如果按照Web应用认证(https://msdn.microsoft.com/en-us/library/mt143610.aspx)进行认证,则满足client_secret
要求。
所以我使用了端点 https://login.microsoftonline.com/<tenantId>/oauth2/token
从 Power BI 获取访问 token 并使用 Java 制作控制台应用程序示例。
这是我的 Maven 依赖项配置,位于 pom.xml
下面文件。
使用1.0.0版本的Azure adal4j没问题,但建议更新最新版本。
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>adal4j</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
</dependencies>
示例代码如下。
String clientId = "<clientId>";
String clientSecret = "<key>";
String tenantId = "<tenantId>";
String authority =String.format("https://login.microsoftonline.com/%s/oauth2/token", tenantId);
String resource = "https://analysis.windows.net/powerbi/api";
AuthenticationContext context = null;
AuthenticationResult result = null;
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(1);
context = new AuthenticationContext(authority, false, service);
ClientCredential credential = new ClientCredential(clientId, clientSecret);
Future<AuthenticationResult> future = context.acquireToken(resource, credential, null);
result = future.get();
} finally {
service.shutdown();
}
String accessToken = null;
if (result == null) {
throw new ServiceUnavailableException("authentication result was null");
} else {
accessToken = result.getAccessToken();
System.out.println("Access Token: " + accessToken);
}
如有任何疑问,请随时告诉我。
<小时/>C#更新:
public static string GetAccessToken()
{
var authenticationContext = new AuthenticationContext("{authorityURL}");
var credential = new ClientCredential(clientId: "{application id}", clientSecret: "{application password}");
var result = authenticationContext.AcquireToken(resource: "{resourceURL}", clientCredential:credential);
if (result == null) {
throw new InvalidOperationException("Failed to obtain the JWT token");
}
string token = result.AccessToken;
return token;
}
请参阅 AuthenticationContext.AcquireToken
的引用https://msdn.microsoft.com/en-us/library/dn479466.aspx .
关于java - 从 Power BI 获取 accessToken,无需 GUI 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33922731/