Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
n02378281
Regular Visitor

Generate PowerBI Embed Token gives me Forbidden/500 Internal Server Error

I have a Native App. I am trying to Embed (App Owns Data) power bi content to my Native App. I am trying to embed a report/ dashboard. To do so I have to generate the Embed Token. When I try to generate the embed token it gives me 500 internal Server error or Forbidden status. Please see the attached error information. What could the reason for this error ? Is this because of Access issue?

 

EmbedToken Error.PNG

 

Below is my code:

public async Task<ActionResult> GetReportByID(String Id)
{

var error = GetWebConfigErrors();
if (error != null)
{
return View(new EmbedConfig()
{
ErrorMessage = error
});
}

// Create a user password cradentials.
var credential = new UserPasswordCredential(Username, Password);

// Authenticate using created credentials
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);

if (authenticationResult == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Authentication Failed."
});
}

var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");

// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
// Get a list of reports.
var reports = await client.Reports.GetReportsInGroupAsync(GroupId);

var report = reports.Value.FirstOrDefault();
// Get the first report in the group.
foreach (var item in reports.Value)
{
if (Id == item.Id)
{
report = item;
}
}

if (report == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Group has no reports."
});
}


// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, Id, generateTokenRequestParameters);

if (tokenResponse == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Failed to generate embed token."
});
}

// Generate Embed Configuration.
var embedConfig = new EmbedConfig()
{
EmbedToken = tokenResponse,
EmbedUrl = report.EmbedUrl,
Id = report.Id
};

return View(embedConfig);
}

}

1 ACCEPTED SOLUTION
Eric_Zhang
Microsoft Employee
Microsoft Employee


@n02378281 wrote:

I have a Native App. I am trying to Embed (App Owns Data) power bi content to my Native App. I am trying to embed a report/ dashboard. To do so I have to generate the Embed Token. When I try to generate the embed token it gives me 500 internal Server error or Forbidden status. Please see the attached error information. What could the reason for this error ? Is this because of Access issue?

 

EmbedToken Error.PNG

 

Below is my code:

public async Task<ActionResult> GetReportByID(String Id)
{

var error = GetWebConfigErrors();
if (error != null)
{
return View(new EmbedConfig()
{
ErrorMessage = error
});
}

// Create a user password cradentials.
var credential = new UserPasswordCredential(Username, Password);

// Authenticate using created credentials
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);

if (authenticationResult == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Authentication Failed."
});
}

var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");

// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
// Get a list of reports.
var reports = await client.Reports.GetReportsInGroupAsync(GroupId);

var report = reports.Value.FirstOrDefault();
// Get the first report in the group.
foreach (var item in reports.Value)
{
if (Id == item.Id)
{
report = item;
}
}

if (report == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Group has no reports."
});
}


// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, Id, generateTokenRequestParameters);

if (tokenResponse == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Failed to generate embed token."
});
}

// Generate Embed Configuration.
var embedConfig = new EmbedConfig()
{
EmbedToken = tokenResponse,
EmbedUrl = report.EmbedUrl,
Id = report.Id
};

return View(embedConfig);
}

}


@n02378281

The above code works in my test. Usually Forbidden error indicates the registered app doesn't have suffiecient permission, please check premissions in azure portal. For other issues, to have a better troubleshooting, please add try..catch block to get more detailed error information.

                try
                {
                    // Generate Embed Token.
                    var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                    var  tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, report.Id, generateTokenRequestParameters);

                    if (tokenResponse == null)
                    {
                        return View(new EmbedConfig()
                        {
                            ErrorMessage = "Failed to generate embed token."
                        });
                    }

                    // Generate Embed Configuration.
                    var embedConfig = new EmbedConfig()
                    {
                        EmbedToken = tokenResponse,
                        EmbedUrl = report.EmbedUrl,
                        Id = report.Id
                    };

                    return View(embedConfig);
                }

                catch (HttpOperationException ex)
                {
                    //debug content for error details
var content = ex.Response.Content; }

By the way, when playing with Power BI API lib, I'd like to test the correlated rest api(GenerateToken in this case) with Postman at first.

View solution in original post

1 REPLY 1
Eric_Zhang
Microsoft Employee
Microsoft Employee


@n02378281 wrote:

I have a Native App. I am trying to Embed (App Owns Data) power bi content to my Native App. I am trying to embed a report/ dashboard. To do so I have to generate the Embed Token. When I try to generate the embed token it gives me 500 internal Server error or Forbidden status. Please see the attached error information. What could the reason for this error ? Is this because of Access issue?

 

EmbedToken Error.PNG

 

Below is my code:

public async Task<ActionResult> GetReportByID(String Id)
{

var error = GetWebConfigErrors();
if (error != null)
{
return View(new EmbedConfig()
{
ErrorMessage = error
});
}

// Create a user password cradentials.
var credential = new UserPasswordCredential(Username, Password);

// Authenticate using created credentials
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, ClientId, credential);

if (authenticationResult == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Authentication Failed."
});
}

var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");

// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
// Get a list of reports.
var reports = await client.Reports.GetReportsInGroupAsync(GroupId);

var report = reports.Value.FirstOrDefault();
// Get the first report in the group.
foreach (var item in reports.Value)
{
if (Id == item.Id)
{
report = item;
}
}

if (report == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Group has no reports."
});
}


// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, Id, generateTokenRequestParameters);

if (tokenResponse == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Failed to generate embed token."
});
}

// Generate Embed Configuration.
var embedConfig = new EmbedConfig()
{
EmbedToken = tokenResponse,
EmbedUrl = report.EmbedUrl,
Id = report.Id
};

return View(embedConfig);
}

}


@n02378281

The above code works in my test. Usually Forbidden error indicates the registered app doesn't have suffiecient permission, please check premissions in azure portal. For other issues, to have a better troubleshooting, please add try..catch block to get more detailed error information.

                try
                {
                    // Generate Embed Token.
                    var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                    var  tokenResponse = await client.Reports.GenerateTokenInGroupAsync(GroupId, report.Id, generateTokenRequestParameters);

                    if (tokenResponse == null)
                    {
                        return View(new EmbedConfig()
                        {
                            ErrorMessage = "Failed to generate embed token."
                        });
                    }

                    // Generate Embed Configuration.
                    var embedConfig = new EmbedConfig()
                    {
                        EmbedToken = tokenResponse,
                        EmbedUrl = report.EmbedUrl,
                        Id = report.Id
                    };

                    return View(embedConfig);
                }

                catch (HttpOperationException ex)
                {
                    //debug content for error details
var content = ex.Response.Content; }

By the way, when playing with Power BI API lib, I'd like to test the correlated rest api(GenerateToken in this case) with Postman at first.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.