cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
adamwallace3 Frequent Visitor
Frequent Visitor

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

Accepted Solutions
Highlighted
adamwallace3 Frequent Visitor
Frequent Visitor

Re: Post Request Embed Token

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)

 

2 REPLIES 2
Moderator Eric_Zhang
Moderator

Re: Post Request Embed Token


@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.

 

Highlighted
adamwallace3 Frequent Visitor
Frequent Visitor

Re: Post Request Embed Token

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)