I am trying to embed a Power BI report to a custom asp.net application. I have followed Microsoft's developer guidance, but I am hitting some odd issues that are not documented very well on the web.
Here is my current code:
using System;
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.Rest;
using Microsoft.PowerBI.Api;
using Microsoft.PowerBI.Api.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Collections.Generic;
using Microsoft.Identity;
using System.Web;
using System.Security.Cryptography.X509Certificates;
namespace AppOnlyAuthPBI.Models
{
public class PBIEmbeddedManager
{
private static string resourceUriPowerBi = "https://analysis.windows.net/powerbi/api";
private static string urlPowerBiRestApiRoot = "https://api.powerbi.com/";
const string aadRootAuthorizationEndpoint = "https://login.windows.net/";
static readonly string tenantId = ConfigurationManager.AppSettings["tenant-id"];
static readonly string aadTenantAuthorizationEndpoint = aadRootAuthorizationEndpoint +
tenantId + "/";
private static string applicationId = ConfigurationManager.AppSettings["application-id"];
private static string applicationSecret = ConfigurationManager.AppSettings["application-secret"];
private static string workspaceId = ConfigurationManager.AppSettings["app-workspace-id"];
private static string reportId = ConfigurationManager.AppSettings["report-id"];
static string GetAccessToken()
{
var authContext = new AuthenticationContext(aadTenantAuthorizationEndpoint);
var clientCredential = new ClientCredential(applicationId, applicationSecret);
return authContext.AcquireTokenAsync(resourceUriPowerBi, clientCredential).Result.AccessToken;
}
private static PowerBIClient GetPowerBiClient()
{
var tokenCredentials = new TokenCredentials(GetAccessToken(), "Bearer");
return new PowerBIClient(new Uri(urlPowerBiRestApiRoot), tokenCredentials);
}
public static async Task<ReportEmbeddingData> GetReportEmbeddingData()
{
PowerBIClient pbiClient = GetPowerBiClient();
var report = await pbiClient.Reports.GetReportInGroupAsync(workspaceId, reportId);
var embedUrl = report.EmbedUrl;
var reportName = report.Name;
GenerateTokenRequest generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
string embedToken =
(await pbiClient.Reports.GenerateTokenInGroupAsync(workspaceId,
report.Id,
generateTokenRequestParameters)).Token;
return new ReportEmbeddingData
{
reportId = reportId,
reportName = reportName,
embedUrl = embedUrl,
accessToken = embedToken
};
}
}
}
I am having issues ReportEmbeddingData() method.
var report = await pbiClient.Reports.GetReportInGroupAsync(workspaceId, reportId);
Here I am getting two errors, both stating cannot convert string to System.Guid. So I tried to parse them like so...
Guid.Parse(workspaceId)
Guid.Parse(reportId)
But, doing this then causes another error to appear which has no information surrounding it on the web...
string embedToken =
(await pbiClient.Reports.GenerateTokenInGroupAsync(workspaceId,
report.Id,
generateTokenRequestParameters)).Token;
I get an error on Reports.GenerateTokenInGroupAsync saying the following: "CS1929 - 'IReportsOperations' does not contain a definition for for 'GenerateTokenInGroupAsync' and the best extension method overload 'IDashboardOperationExtensions, guid, guid, GenerateTokenPermissions, CancellationToken' requires a receiver type of 'IDashboardsOperation'
To me it seems like I am missing a nuGet package, but I have verified this and everything is working as intended there and I have the required using statements applied as well.
Has anyone encountered this or does anyone know how to correct this? It seems really odd to me that Microsoft is recommending this code and I am hitting an error, leading me to believe that I am the one making the mistake but it is alluding me.
Thank you~