对于 ADAL,第一步是注册应用程序。在 GitHub here 的 PowerBI-Developers-Repo 的自述文件中有一个涵盖该主题的示例。创建应用程序注册后,ADAL 令牌获取可能会有所不同,具体取决于您是尝试获取仅应用程序令牌还是用户令牌。这取决于您是否在应用程序注册中授予它应用程序权限(服务或守护程序,对数据的完全访问权限)或委托权限(用户范围,API 只能访问当前用户拥有的权限)。这些概念在here 中进行了详细讨论。
因此,纯粹使用 REST 委托代币获取会很困难,因为 ADAL 需要与用户交互。但是,为了获得良好的概念证明,App-only 令牌用于非交互。在 Microsoft Docs for Azure Active Directory 中记录了 here。该收购将是一个简单的POST,如下所示:
POST <https://login.microsoftonline.com/{tenant}/oauth2/token> HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
&client_id={application id from the Application registration}
&client_secret={Application Key from Azure AD registration}
&resource=https%3A%2F%analysis.windows.net/powerbi/api%2F
有关该电话的完整详细信息,请访问 here。
对于用户令牌,您可以这样做:
// Line breaks for legibility only
https://login.microsoftonline.com/{tenant}/oauth2/authorize?
client_id={Application ID from the registration}
&response_type=token
&redirect_uri={URL encoded redirect from application registration}
&response_mode=query
&resource=https%3A%2F%analysis.windows.net/powerbi/api%2F
&state=12345
这样做的问题是它将为用户提供交互式登录,因此在这种情况下并不是真正的 REST。如果您刚刚在浏览器中浏览到该 URL,将 {tenant} 替换为您的租户名称或 ID,则重定向将包含一个 URL 参数 access_token,这将是您的 JWT 令牌。但是对于每个 REST 测试和学习,我建议首先尝试 App-only token 方法。完成后,只需将 Header: Authorization: bearer access_token 添加到 API 的 REST 调用中。