无需GUI身份验证即可从power BI获取accessToken



我遵循了power BI web应用程序身份验证文档中提供的说明,能够获得身份验证令牌。但该过程要求用户在GUI中输入凭据以获得身份验证代码。

相反,我想进行静默身份验证,即不进行GUI身份验证。我尝试了几种方法,比如在下面的链接中使用HHTPClient和参考代码

https://gist.github.com/dquig/a4f2f02fe3e306cebe2e

但它要求client_secret,不幸的是,没有方法接受cleint_secret以及resourceuri、clientId、username、password和callback。am使用1.0.0版本的azure adal4j。这是版本问题吗?任何帮助都将大大节省

由于使用客户端应用程序获取Azure AD访问令牌,因此需要在GUI身份验证中输入用户名和密码。如果遵循web应用程序身份验证(https://msdn.microsoft.com/en-us/library/mt143610.aspx)进行身份验证,满足client_secret的要求。

因此,我使用端点https://login.microsoftonline.com/<tenantId>/oauth2/token从Power BI获取访问令牌,并用Java制作了一个控制台应用程序示例。

下面是我在pom.xml文件中的maven依赖项配置。使用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.

我想你应该看看这篇博客文章:http://www.cloudidentity.com/blog/2014/07/08/using-adal-net-to-authenticate-users-via-usernamepassword/

ADAL.NET客户端库API现在具有接受用户名和密码的方法。这篇文章列出了所有的限制和限制。如果您的使用不符合满意的路径,我认为您需要提示用户。

我应该提到的是,如果您为用户存储刷新令牌,那么您只需要每90天提示一次(或者每当刷新令牌过期时)。

如果您将应用程序注册为本机客户端应用程序,则需要提供客户端机密。然后,您就可以使用用户名和密码流,而无需进行交互式登录。

最新更新