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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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