我正在使用Auth0,单页Angular应用程序。
我想调用其他已授予的API。我的身份验证如下:
我还需要PowerBI令牌
过去,我是使用纯AzureAD和相关的JavaScript库完成此操作的,基本上,您需要为正确的受众“获取”令牌。
如何获取PowerBI Audience的令牌?我尝试了以下代码
this.auth.getTokenSilently$(
{audience: 'https://analysis.windows.net/powerbi/api'} ).pipe( tap( t => console.log('Token', t)) ).subscribe();
但是该令牌不是与PowerBI通用的令牌
我的补助金
经过更多查看后,您似乎无法从Auth0获得正确的令牌,如果我从user.identities[0];
获得了令牌,则该令牌看起来像是一个有效的令牌,但是它不是由AzureAD发出的,因此您无法使用它来生成Power BI令牌。
这就是auth0
给我的
这是我使用ADAL JS时得到的
然后我可以将上面的令牌和一些c#代码一起用于令牌,例如
var cred = new ClientCredential(clientId, secret);
var access_token_from_client_side = token;
var context = new AuthenticationContext("https://login.windows.net/common");
var res = context.AcquireTokenAsync("https://analysis.windows.net/powerbi/api",
cred, new Microsoft.IdentityModel.Clients.ActiveDirectory.UserAssertion(access_token_from_client_side, "urn:ietf:params:oauth:grant-type:jwt-bearer")).ConfigureAwait(false).GetAwaiter().GetResult();
或者,我可以在标准ADAL JS身份验证流程中使用一些简单的回调。
authContext.acquireToken(
'https://analysis.windows.net/powerbi/api',
function (error, token) {
if (error || !token) {
// TODO: Handle error obtaining access token
document.getElementById('api_response').textContent =
'ERROR:\n\n' + error;
return;
}
console.log('report token',token);
});
这些工作环境并不是真正有用,因为我不想使用ADAL和AUTH0
如何从auth0获得可使用的令牌: