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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
adamwallace3
Advocate II
Advocate II

Post Request Embed Token

Hi, 

 

I am struggling to adapt any of the example code to silently get an embed token. My web app is built on the Django framework with an app owns data model (with RLS). 

 

Is there a POST/GET solution that is similar to the the CLI tool that I can use? Or is it possible to build an embed token outright? 

 

I am sorry if I this is a silly question, I am quite new to Python and Web apps.

 

Thanks in advance.

1 ACCEPTED SOLUTION

I actually was able to figure it out with the help of a Microsoft Case Study I found. 

 

You don't need any kind of API for the embed token itself, you can build the JWT outright if you already have an access key.

 

 

import time
import jwt

def generate_token(access_key, workspace_id, report_id, workspace_collection, brand):
    """Generate JWT token."""
    # token times
    start = time.time()
    end = start + 60 * 60

    # create tokens
    headers = {
        "typ": "JWT",
        "alg": "HS256",
}
    payload = {
        "wid": workspace_id,
        "rid": report_id,
        "wcn": workspace_collection,
        "username": VALID USERNAME,
        "roles": [ROLE1, ROLE2, ETC],
        "iss": "PowerBISDK",
        "ver": "0.2.0",
        "aud": "https://analysis.windows.net/powerbi/api",
        "nbf": start,
        "exp": end,
    }
    return jwt.encode(key=access_key, headers=headers, payload=payload)

 

View solution in original post

2 REPLIES 2
Eric_Zhang
Employee
Employee


@adamwallace3 wrote:

Hi, 

 

I am struggling to adapt any of the example code to silently get an embed token. My web app is built on the Django framework with an app owns data model (with RLS). 

 

Is there a POST/GET solution that is similar to the the CLI tool that I can use? Or is it possible to build an embed token outright? 

 

I am sorry if I this is a silly question, I am quite new to Python and Web apps.

 

Thanks in advance.


@adamwallace3

 

You can use below POST REST API to get the access Bearer token.

 

POST /common/oauth2/token HTTP/1.1
Host: login.windows.net
Cache-Control: no-cache 
Content-Type: application/x-www-form-urlencoded

client_id={client_id}&grant_type=password&resource=https://analysis.windows.net/powerbi/api&username={powerbi account}&password={powerbi account password}

 

And then use this API GenerateToken to get embedded token for dashboards/reports/datasets. Note that it seems the RLS now is only avalaible for embedded reports not for dashboards. See this thread.

 

By the way, when using App Owns data, you'll have to purchase Power BI Premium license when promoting your solution to production.

 

I actually was able to figure it out with the help of a Microsoft Case Study I found. 

 

You don't need any kind of API for the embed token itself, you can build the JWT outright if you already have an access key.

 

 

import time
import jwt

def generate_token(access_key, workspace_id, report_id, workspace_collection, brand):
    """Generate JWT token."""
    # token times
    start = time.time()
    end = start + 60 * 60

    # create tokens
    headers = {
        "typ": "JWT",
        "alg": "HS256",
}
    payload = {
        "wid": workspace_id,
        "rid": report_id,
        "wcn": workspace_collection,
        "username": VALID USERNAME,
        "roles": [ROLE1, ROLE2, ETC],
        "iss": "PowerBISDK",
        "ver": "0.2.0",
        "aud": "https://analysis.windows.net/powerbi/api",
        "nbf": start,
        "exp": end,
    }
    return jwt.encode(key=access_key, headers=headers, payload=payload)

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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