我正在实现一个C#API,该API在AAD中进行身份验证,并通过Service Principal返回针对不同报告的嵌入令牌。如果该报告使用存储在Google大查询中的数据可以正常工作,但是,我的同事开始与Azure Analysis Services一起尝试离开GBQ,并设法连接PowerBi和AAS并通过此方法发布报告。
如果我们使用PowerBI Online,我们都可以访问该报告,但是如果我尝试使用我的api为该报告生成嵌入令牌,则会出现以下错误:
我们是否缺少api访问该报告所需的某种权限?我没有找到任何说明使用AAS时嵌入过程发生变化的信息。该api的代码如下所示,我知道它在GenerateTokenRequest对象中缺少一些字段,我认为这就是问题所在,但我还没有机会对其进行测试。
public class PBIController : ApiController
{
private static readonly string Tenant = "tenant";
private static readonly string AppId = "id";
private static readonly string AppSecret = "secret";
private static readonly string Resource = "https://analysis.windows.net/powerbi/api";
private string getToken()
{
var authContext = new AuthenticationContext(Tenant);
var clientCredential = new ClientCredential(AppId, AppSecret);
return authContext.AcquireTokenAsync(Resource, clientCredential).Result.AccessToken;
}
private PowerBIClient getPBIClient()
{
var token = new TokenCredentials(getToken(), "Bearer");
return new PowerBIClient(new Uri("https://api.powerbi.com/"), token);
}
public async Task<ODataResponseListReport> getReports(string ws)
{
PowerBIClient pbiClient = getPBIClient();
var reports = await pbiClient.Reports.GetReportsInGroupAsync(ws);
return reports;
}
public async Task<ReportEmbeddingData> getReportEmbeddingData(string ws, string rep)
{
PowerBIClient pbiClient = getPBIClient();
var report = await pbiClient.Reports.GetReportInGroupAsync(ws, rep);
var embedUrl = report.EmbedUrl;
var reportName = report.Name;
GenerateTokenRequest genTokenReqParam = new GenerateTokenRequest(accessLevel:"view");
string embedToken = (await pbiClient.Reports.GenerateTokenInGroupAsync(ws, rep, genTokenReqParam)).Token;
return new ReportEmbeddingData
{
type = "report",
reportId = rep,
reportName = reportName,
embedUrl = embedUrl,
accessToken = embedToken
};
}
}