使用Power BI嵌入令牌时如何解决‘NotFound’无效状态码错误?
Power BI嵌入令牌生成触发NotFound异常的排查方案
异常信息
Microsoft.Rest.HttpOperationException: Operation returned an invalid status code 'NotFound' at Microsoft.PowerBI.Api.ReportsOperations.<GenerateTokenInGroupWithHttpMessagesAsync>d__32.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.PowerBI.Api.ReportsOperationsExtensions.<GenerateTokenInGroupAsync>d__87.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.PowerBI.Api.ReportsOperationsExtensions.GenerateTokenInGroup(IReportsOperations operations, Guid groupId, Guid reportId, GenerateTokenRequest requestParameters) at EmbedAPISample.Program.Main(String[] args) in E:\Testes de Codigo\CSharp\PowerBI\Token\Embed-API-Sample\EmbedAPISample\Program.cs:line 53
核心代码片段
class Program { private static bool useEmbedToken = true; private static bool useRLS = false; private static string authorityUrl = "https://login.microsoftonline.com/organizations/"; private static string resourceUrl = "https://analysis.windows.net/powerbi/api"; private static string apiUrl = "https://api.powerbi.com/"; private static string tenantId = "tenant id"; private static Guid groupId = Guid.Parse("group id"); private static Guid reportId = Guid.Parse("report id"); private static Guid datasetId = Guid.Parse("data set id"); // **** Update the Client ID and Secret within Secrets.cs **** private static ClientCredential credential = null; private static AuthenticationResult authenticationResult = null; private static TokenCredentials tokenCredentials = null; static void Main(string[] args) { try { // Create a user password cradentials. credential = new ClientCredential(Secrets.ClientID, Secrets.ClientSecret); // Authenticate using created credentials Authorize().Wait(); using (var client = new PowerBIClient(new Uri(apiUrl), tokenCredentials)) { #region Embed Token EmbedToken embedToken = null; if (useEmbedToken && !useRLS) { // **** Without RLS **** embedToken = client.Reports.GenerateTokenInGroup(groupId, reportId, new GenerateTokenRequest(accessLevel: "View", datasetId: datasetId.ToString())) ; } else if(useEmbedToken && useRLS) { // 其余代码省略 } } } catch(Exception ex) { // 异常处理代码 } } }
排查步骤
- 验证ID参数准确性:
- 核对groupId、reportId、datasetId与Power BI服务中的实际值,确保无大小写错误、空格或格式偏差。可从对应资源的浏览器地址栏直接复制ID。
- 检查应用工作区权限:
- 将注册的Azure AD应用添加为目标工作区的成员或管理员,确保应用能访问该工作区内的报表和数据集。
- 确认嵌入容量关联:
- 检查目标工作区是否已分配到创建的嵌入容量(Premium/Embedded),未关联容量可能导致令牌生成失败。
- 验证API权限配置:
- 确认应用已被授予
Report.Read.All或Report.ReadWrite.All的应用权限,且管理员已完成权限同意。
- 确认应用已被授予
- 调试认证流程:
- 在
Authorize()方法中添加日志,确认获取的认证令牌有效,受众为https://analysis.windows.net/powerbi/api。
- 在
- 检查SDK版本:
- 使用最新稳定版的Power BI .NET SDK,避免旧版本的API兼容性问题。
内容的提问来源于stack exchange,提问作者Igor Foggiatto
相关产品推荐
相关产品推荐

