Forum Discussion

n02378281's avatar
n02378281
Regular Visitor
9 years ago
Solved

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?

 

 

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 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?

     

     

    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.

1 Reply

  • Eric_Zhang's avatar
    Eric_Zhang
    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?

     

     

    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.