Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hi, I've tried many ways (Powershell, Azure Function, etc.) to get the Embed Token. But none of that works, I always received Forbidden (403). Below is my code in Azure, Function. I obtained AccessToken successfully, which means I am authorized. I received the Embed URL too. However, the step always fails at generate Embed Token's step.
#r "System.Web.Extensions" using System.Configuration; using System.Net; using System.Text; using System.Web.Script.Serialization; using Microsoft.IdentityModel.Clients.ActiveDirectory; using Microsoft.PowerBI.Api.V2; using Microsoft.PowerBI.Api.V2.Models; using Microsoft.Rest; // Static Values static string authorityUrl = "https://login.windows.net/common/oauth2/authorize/"; static string resourceUrl = "https://analysis.windows.net/powerbi/api"; static string apiUrl = "https://api.powerbi.com/"; static string clientId = ConfigurationManager.AppSettings["PBIE_CLIENT_ID"]; static string username = ConfigurationManager.AppSettings["PBIE_USERNAME"]; static string password = ConfigurationManager.AppSettings["PBIE_PASSWORD"]; static string groupId = ConfigurationManager.AppSettings["PBIE_GROUP_ID"]; static string reportId = ConfigurationManager.AppSettings["PBIE_REPORT_ID"]; public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) { // Authenticate with Azure Ad > Get Access Token > Get Token Credentials var credential = new UserPasswordCredential(username, password); string myCredential = credential.ToString(); var authenticationContext = new AuthenticationContext(authorityUrl); var authenticationResult = await authenticationContext.AcquireTokenAsync(resourceUrl, clientId, credential); string accessToken = authenticationResult.AccessToken; log.Info(accessToken); var tokenCredentials = new TokenCredentials(accessToken, "Bearer"); using (var client = new PowerBIClient(new Uri(apiUrl), tokenCredentials)) { // Embed URL Report report = client.Reports.GetReportInGroup(groupId, reportId); string embedUrl = report.EmbedUrl; log.Info(embedUrl); // Embed Token (Step that fails!!) var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view"); EmbedToken embedToken = client.Reports.GenerateTokenInGroup(groupId, reportId, generateTokenRequestParameters); // JSON Response EmbedContent data = new EmbedContent(); data.EmbedToken = embedToken.Token; data.EmbedUrl = embedUrl; data.ReportId = reportId; JavaScriptSerializer js = new JavaScriptSerializer(); string jsonp = "callback(" + js.Serialize(data) + ");"; // Return Response return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(jsonp, Encoding.UTF8, "application/json") }; } } public class EmbedContent { public string EmbedToken { get; set; } public string EmbedUrl { get; set; } public string ReportId { get; set; } }
Output: