Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Unable to get Embed Token (Access Token and Embed URL done)

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: 

2019-03-27T03:23:55.435 [Error] Exception while executing function: Functions.getEmbedToken. Microsoft.Azure.WebJobs.Script: One or more errors occurred. Microsoft.PowerBI.Api: Operation returned an invalid status code 'Forbidden'.
2019-03-27T03:23:55.482 [Error] Function completed (Failure, Id=5c8fa500-7bd9-49a5-97e6-975d25eb02fd, Duration=1061ms)
 
 
 
I've spent a lot of time researching for this problem. Hope someone could help me on this. Appreciate much. Thanks in advance.
0 REPLIES 0

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.