Last active
August 15, 2019 20:42
-
-
Save cbattlegear/56aa62bd4a23084d95b9332c219cb720 to your computer and use it in GitHub Desktop.
An azure function to get a power bi embedded token via rest api call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"WorkspaceId":"6173e6bf-f90a-4e7a-9f97-b3672e7b9fa5", | |
"ReportId":"b2309d7f-5347-4e97-aab5-04f68eacc7be" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"frameworks": { | |
"net46":{ | |
"dependencies": { | |
"Microsoft.IdentityModel.Clients.ActiveDirectory": "4.5.0", | |
"Microsoft.PowerBI.Api": "2.7.0", | |
"Microsoft.Rest.ClientRuntime": "2.3.18" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Newtonsoft.Json" | |
using System.Net; | |
using Microsoft.IdentityModel.Clients.ActiveDirectory; | |
using Microsoft.PowerBI.Api.V2; | |
using Microsoft.PowerBI.Api.V2.Models; | |
using Microsoft.Rest; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using System.Text; | |
//Change these settings to fit your system | |
private static readonly string ApplicationId = "YourAppIdGuidHere"; | |
private static readonly string ClientSecret = "YourClientSecretHere"; | |
private static readonly string TenantId = "YourTenantIdHere"; | |
private static readonly string AuthorityUrl = "https://login.windows.net/common/"; | |
private static readonly string ResourceUrl = "https://analysis.windows.net/powerbi/api"; | |
private static readonly string ApiUrl = "https://api.powerbi.com/"; | |
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info("Get PowerBI Token trigger function processed a request."); | |
dynamic data = await req.Content.ReadAsAsync<object>(); | |
string WorkspaceId = data?.WorkspaceId; | |
string ReportId = data?.ReportId; | |
string json_out = JsonConvert.SerializeObject(await EmbedDashboard(WorkspaceId, ReportId), Formatting.Indented); | |
return new HttpResponseMessage(HttpStatusCode.OK) | |
{ | |
Content = new StringContent(json_out, Encoding.UTF8, "application/json") | |
}; | |
} | |
public static async Task<EmbedConfig> EmbedDashboard(string WorkspaceId, string ReportId = null) | |
{ | |
// Create client creds | |
var credential = new ClientCredential(ApplicationId, ClientSecret); | |
// Authenticate using created credentials | |
var authenticationContext = new AuthenticationContext(AuthorityUrl.Replace("common", TenantId)); | |
var authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, credential); | |
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 dashboards. | |
var reports = await client.Reports.GetReportsInGroupAsync(WorkspaceId); | |
// No reports retrieved for the given workspace. | |
if (reports.Value.Count() == 0) | |
{ | |
} | |
Report report; | |
if (string.IsNullOrWhiteSpace(ReportId)) | |
{ | |
// Get the first report in the workspace. | |
report = reports.Value.FirstOrDefault(); | |
} | |
else | |
{ | |
report = reports.Value.FirstOrDefault(r => r.Id.Equals(ReportId, StringComparison.InvariantCultureIgnoreCase)); | |
} | |
// Generate Embed Token. | |
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view"); | |
var tokenResponse = await client.Reports.GenerateTokenInGroupAsync(WorkspaceId, report.Id, generateTokenRequestParameters); | |
// Generate Embed Configuration. | |
var embedConfig = new EmbedConfig() | |
{ | |
EmbedToken = tokenResponse, | |
EmbedUrl = report.EmbedUrl, | |
Id = report.Id | |
}; | |
return embedConfig; | |
} | |
} | |
public class EmbedConfig | |
{ | |
public string Id { get; set; } | |
public string EmbedUrl { get; set; } | |
public EmbedToken EmbedToken { get; set; } | |
public int MinutesToExpiration | |
{ | |
get | |
{ | |
var minutesToExpiration = EmbedToken.Expiration.Value - DateTime.UtcNow; | |
return minutesToExpiration.Minutes; | |
} | |
} | |
public bool? IsEffectiveIdentityRolesRequired { get; set; } | |
public bool? IsEffectiveIdentityRequired { get; set; } | |
public bool EnableRLS { get; set; } | |
public string Username { get; set; } | |
public string Roles { get; set; } | |
public string ErrorMessage { get; internal set; } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment