public ActionResult Index(string reportId) { // use app key to create credentials used to generate embed tokens TokenCredentials credentials = new TokenCredentials(accessKey, "AppKey"); PowerBIClient client = new PowerBIClient(credentials); // call to Power BI REST API to get list of reports inside a specific worksapce IList <Report> reports = client.Reports.GetReportsAsync(workspaceCollection, workspaceId).Result.Value; var viewModel = new ReportsViewModel { Reports = reports.ToList() }; if (!string.IsNullOrEmpty(reportId)) { Report report = reports.Where(r => r.Id == reportId).First(); PowerBIToken embedToken = PowerBIToken.CreateReportEmbedToken(workspaceCollection, workspaceId, report.Id); viewModel.CurrentReport = new ReportViewModel { Report = report, AccessToken = embedToken.Generate(accessKey) }; } return(View(viewModel)); }
public void CanManuallyCreatePowerBIToken() { var token = new PowerBIToken { Audience = "TestAudience", Issuer = "TestIssuer", Expiration = DateTime.UtcNow.AddHours(2), AccessKey = this.accessKey }; token.Claims.Add(new Claim("Name", "TestUser")); var jwt = token.Generate(); var decodedToken = new JwtSecurityToken(jwt); var tokenExpiration = new DateTime( token.Expiration.Value.Year, token.Expiration.Value.Month, token.Expiration.Value.Day, token.Expiration.Value.Hour, token.Expiration.Value.Minute, token.Expiration.Value.Second); Assert.IsTrue(decodedToken.Audiences.Contains(token.Audience)); Assert.AreEqual(token.Issuer, decodedToken.Issuer); Assert.AreEqual(tokenExpiration, decodedToken.ValidTo); var nameClaim = decodedToken.Claims.FirstOrDefault(c => c.Type == "Name"); Assert.AreEqual("TestUser", nameClaim.Value); }
/// <summary> /// Creates a new instance of the PowerBIClient with the specified token /// </summary> /// <param name="token">A Power BI app token</param> /// <returns></returns> static async Task <IPowerBIClient> CreateClient(PowerBIToken token) { if (accessKeys == null) { Console.Write("Access Key: "); accessKey = Console.ReadLine(); Console.WriteLine(); accessKeys = new WorkspaceCollectionKeys() { Key1 = accessKey }; } if (accessKeys == null) { accessKeys = await ListWorkspaceCollectionKeys(subscriptionId, resourceGroup, workspaceCollectionName); } // Generate a JWT token used when accessing the REST APIs var jwt = token.Generate(accessKeys.Key1); // Create a token credentials with "AppToken" type var credentials = new TokenCredentials(jwt, "AppToken"); // Instantiate your Power BI client passing in the required credentials var client = new PowerBIClient(credentials); // Override the api endpoint base URL. Default value is https://api.powerbi.com client.BaseUri = new Uri(apiEndpointUri); return(client); }
public async Task <ActionResult> Report(string reportId, string userName, string rolesCSV) { using (var client = this.CreatePowerBIClient()) { var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId); var report = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId); PowerBIToken embedToken = null; var userNameResolve = string.IsNullOrWhiteSpace(userName) ? null : userName.Trim(); var roles = string.IsNullOrWhiteSpace(rolesCSV) ? null : rolesCSV.Split(new char[] { ',' }).Select(r => r.Trim()); embedToken = PowerBIToken.CreateReportEmbedToken( workspaceCollectionName: this.workspaceCollection, workspaceId: this.workspaceId, reportId: report.Id, username: userNameResolve, roles: roles ); var viewModel = new ReportViewModel { Report = report, AccessToken = embedToken.Generate(this.accessKey) }; return(View(viewModel)); } }
public void CreateGenericTokenWithMissingParamsFails() { var token = new PowerBIToken { Issuer = null, Audience = null }; token.Generate(this.accessKey); }
public void GetEmbedToken() { foreach (var report in powerbiReports) { PowerBIToken token = PowerBIToken.CreateReportEmbedToken(collectionName, currentWorkSpace.WorkspaceId, report.Id); var jwt = token.Generate(accessKey); Console.WriteLine("report {0}, access token: {1}", report.Name, jwt); } }
private IPowerBIClient CreatePowerBIClient(PowerBIToken token) { var jwt = token.Generate(_signingKey); var credentials = new TokenCredentials(jwt, "AppToken"); var client = new PowerBIClient(credentials) { BaseUri = new Uri(_apiUrl) }; return(client); }
private IPowerBIClient CreatePowerBIClient(PowerBIToken token) { var jasonWebToken = token.Generate(accessKey); var credentials = new TokenCredentials(jasonWebToken, "AppToken"); var client = new PowerBIClient(credentials) { BaseUri = new Uri(apiUrl) }; return(client); }
private IPowerBIClient CreatePowerBIClient(PowerBIToken token) { var jwt = token.Generate(ConfigurationManager.AppSettings["powerbi:SigningKey"]); var credentials = new TokenCredentials(jwt, "AppToken"); var client = new PowerBIClient(credentials) { BaseUri = new Uri(ConfigurationManager.AppSettings["powerbi:ApiUrl"]) }; return(client); }
protected IPowerBIClient CreatePowerBIClient(PowerBIToken token) { var jwt = token.Generate(accessKey); var credentials = new TokenCredentials(jwt, "AppToken"); var client = new PowerBIClient(credentials) { BaseUri = new Uri(apiUrl) }; return(client); }
public void CreateGenericTokenWithInvalidExpirationFails() { var token = new PowerBIToken { Issuer = "issuer", Audience = "audience", AccessKey = this.accessKey, Expiration = DateTime.MinValue }; token.Generate(); }
private static IPowerBIClient CreatePowerBiClient(PowerBIToken token) { var jwt = token.Generate(ConfigHelper.PowerbiSigningKey); var credentials = new TokenCredentials(jwt, "AppToken"); var client = new PowerBIClient(credentials) { BaseUri = new Uri(ConfigHelper.PowerbiApiUrl) }; return(client); }
static IPowerBIClient CreateClient(PowerBIToken token, string accessKey, string apiUrl) { WorkspaceCollectionKeys accessKeys = new WorkspaceCollectionKeys() { Key1 = accessKey }; // Generate a JWT token used when accessing the REST APIs var jwt = token.Generate(accessKeys.Key1); // Create a token credentials with "AppToken" type var credentials = new TokenCredentials(jwt, "AppToken"); // Instantiate your Power BI client passing in the required credentials var client = new PowerBIClient(credentials); // Override the api endpoint base URL. Default value is https://api.powerbi.com client.BaseUri = new Uri(apiUrl); return(client); }
public ActionResult Index() { // use app key to create credentials used to retreive developer access token TokenCredentials credentials = new TokenCredentials(accessKey, "AppKey"); PowerBIClient client = new PowerBIClient(credentials); // call to Power BI REST API to get list of reports inside a specific worksapce ODataResponseListReport reportsResult = client.Reports.GetReportsAsync(workspaceCollection, workspaceId).Result; // this sample app is hardcoded to load first report in collection Report report = reportsResult.Value[0]; // create the embed token for the report PowerBIToken embedToken = PowerBIToken.CreateReportEmbedToken(workspaceCollection, workspaceId, report.Id); // Pass report and access token to MVC view for rending ReportViewModel reportModel = new ReportViewModel { Report = report, AccessToken = embedToken.Generate(accessKey) }; return(View(reportModel)); }
static async Task GetEmbedInfo() { EnsureBasicParams(EnsureExtras.WorkspaceCollection | EnsureExtras.WorspaceId); int?index = 1; Console.WriteLine("Select Embed Mode:"); Console.WriteLine("1) View Mode: Report.Read"); Console.WriteLine("2) Edit & Save Mode: Report.ReadWrite"); Console.WriteLine("3) View & Save As Mode: Report.ReadWrite Workspace.Report.Create"); Console.WriteLine("4) Edit & Save As Mode: Report.Read Workspace.Report.Create"); Console.WriteLine("5) Create Report Mode: Dataset.Read Workspace.Report.Create"); int?mode = userInput.EnsureIntParam((int)EmbedMode.View, "Embed mode"); if (!mode.HasValue || mode.Value <= 0 || mode.Value > 5) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("selected mode is out of range."); return; } var embedMode = (EmbedMode)mode.Value; string reportId = null; string datasetId = null; string embedUrl = null; if (embedMode == EmbedMode.View || embedMode == EmbedMode.ViewAndSaveAs || embedMode == EmbedMode.EditAndSaveAs || embedMode == EmbedMode.EditAndSave) { // For these modes user need to select a report to embed var reports = await GetReports(workspaceCollectionName, workspaceId); if (!PrintReports(reports)) { return; } index = userInput.EnsureIntParam(index, "Index of report to use"); if (!index.HasValue || index.Value <= 0 || index.Value > reports.Count) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Report index is out of range."); return; } var report = reports[index.Value - 1]; reportId = report.Id; embedUrl = report.EmbedUrl; } else { // For these modes user need to select a dataset to create a report with var datasets = await GetDatasets(workspaceCollectionName, workspaceId); PrintDatasets(datasets); index = userInput.EnsureIntParam(index, "Index of dataset to create a report with"); if (!index.HasValue || index.Value <= 0 || index.Value > datasets.Count) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Dataset index is out of range."); return; } var dataset = datasets[index.Value - 1]; datasetId = dataset.Id; embedUrl = defaultEmbedUrl; } var scopes = string.Empty; switch (embedMode) { case EmbedMode.View: scopes = "Report.Read"; break; case EmbedMode.EditAndSave: scopes = "Report.ReadWrite"; break; case EmbedMode.ViewAndSaveAs: scopes = "Report.Read Workspace.Report.Create"; break; case EmbedMode.EditAndSaveAs: scopes = "Report.ReadWrite Workspace.Report.Create"; break; case EmbedMode.CreateMode: scopes = "Dataset.Read Workspace.Report.Create"; break; default: scopes = string.Empty; break; } // RLS var rlsUsername = userInput.EnsureParam(null, "RLS - limit to specific user: (Keep empty to create without RLS)"); var rlsRoles = userInput.EnsureParam(null, "RLS - limit to specific roles: (comma separated)"); var roles = string.IsNullOrEmpty(rlsRoles) ? null : rlsRoles.Split(','); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Embed Url: {0}", embedUrl); TimeSpan expiry = new TimeSpan(24, 0, 0); PowerBIToken embedToken = null; if (!string.IsNullOrEmpty(reportId)) { embedToken = PowerBIToken.CreateReportEmbedToken(workspaceCollectionName, workspaceId, reportId, expiry, rlsUsername, roles, scopes); } else if (!string.IsNullOrEmpty(datasetId)) { embedToken = PowerBIToken.CreateReportEmbedTokenForCreation(workspaceCollectionName, workspaceId, datasetId, expiry, rlsUsername, roles, scopes); } var token = embedToken.Generate(accessKeys.Key1); Console.WriteLine("Embed Token: {0}", token); var copy = userInput.EnsureParam(null, "Copy embed token to clipboard? (Y)/(N) "); if (copy.Equals("y", StringComparison.CurrentCultureIgnoreCase)) { try { // Save access token to Clipboard System.Windows.Forms.Clipboard.SetText(token); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Embed Token saved to clipboard."); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Embed Token could not be saved to clipboard."); Console.WriteLine(ex.ToString()); } } }