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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Employee
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
Employee
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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.