GenerateToken

 

Updated: September 13, 2017


Report | Dataset | Dashboard | Tile | Response | Example

The Generate token API allows creation of embed tokens for reports, datasets, dashboards and tiles. The structure of the API is similar for all resources.

Request

POST https://api.powerbi.com/v1.0/myorg/groups/{group_Id}/reports/{report_id}/GenerateToken

Groups

Groups are a collection of unified Azure Active Directory groups that the user is a member of and is available in the Power BI service. These are referred to as app workspaces within the Power BI service. To learn how to create a group, see Create an app workspace.

Uri parameter

NameDescriptionData Type
group_idGuid of the Group to use. You can get the group id from the Get Groups operation. Groups are referred to as app workspaces within the Power BI service.String
report_idGuid of the report to use.String

Header

Content-Type: application/json; charset=utf-8
Authorization: Bearer eyJ0eX ... FWSXfwtQ

The Authorization header is the Power BI AccessToken.

Body

The body is a JSON containing the following.

{   
    "accessLevel": "View",
    "identities": [     
        {      
            "username": "EffectiveIdentity",
            "roles": [ "Role1", "Role2" ],
            "datasets": [ "fe0a1aeb-f6a4-4b27-a2d3-b5df3bb28bdc" ]
        }   
    ] 
} 


For more information on the identities element, and how to use row-level security when embedding a report, see Row-level security (RLS) with embedded analytics.

Request

POST https://api.powerbi.com/v1.0/myorg/groups/{group_Id}/reports/GenerateToken

Uri parameter

NameDescriptionData Type
group_idGuid of the Group to use. You can get the group id from the Get Groups operation. Groups are referred to as app workspaces within the Power BI service.String

Header

Content-Type: application/json; charset=utf-8
Authorization: Bearer eyJ0eX ... FWSXfwtQ

The Authorization header is the Power BI AccessToken.

Body

The body is a JSON containing

{
  "accessLevel": "Create",
  "datasetId": "string",
  "allowSaveAs": true
}

Request

POST https://api.powerbi.com/v1.0/myorg/groups/{group_Id}/dashboards/{dashboard_Id}/GenerateToken

Uri parameter

NameDescriptionData Type
group_IdGuid of the Group to use. You can get the group id from the Get Groups operation. Groups are referred to as app workspaces within the Power BI service.String
dashboard_IdGuid of the dashboard to use.String

Header

Content-Type: application/json; charset=utf-8
Authorization: Bearer eyJ0eX ... FWSXfwtQ

The Authorization header is the Power BI AccessToken.

Body

The body is a JSON containing

{
  "accessLevel": "View"
}

Request

POST https://api.powerbi.com/v1.0/myorg/groups/{group_Id}/dashboards/{dashboard_Id}/tiles/{tile_Id}/GenerateToken

Uri parameter

NameDescriptionData Type
group_IdGuid of the Group to use. You can get the group id from the Get Groups operation. Groups are referred to as app workspaces within the Power BI service.String
dashboard_IdGuid of the dashboard to use.String
tile_IdGuid of the tile to use.String

Header

Content-Type: application/json; charset=utf-8
Authorization: Bearer eyJ0eX ... FWSXfwtQ

The Authorization header is the Power BI AccessToken.

Body

The body is a JSON containing

{
  "accessLevel": "View"
}

Status code

CodeDescription
200Success.

Content-Type

application/json

Body schema

A GenerateToken response has the following schema:

{                
    "expiration":"{expiration}",  
    "token":"{token}",  
    "tokenId":"{tokenId}"  
}

TokenId can be logged and used by the application for auditing purposes.


[Top of article]

This is sample code for a function that generates an EmbedToken for reports. For C#, In order to generate an Embed token, you can use Microsoft.PoawerBI.Api library available as a NuGet package.

async Task<string> generateEmbedToken(string AadToken, string reportId, string groupId,
    string baseUri, string accessLevel = "View", string datasetId = "")
        {
            // TokenCredentials Initializes a new instance of the
            // Microsoft.Rest.TokenCredentials class with
            // the given 'Bearer' token.
            var credentials = new TokenCredentials(AadToken);
 
            // Initialize PowerBIClient with credentials
            var powerBIclient = new PowerBIClient(credentials);
 
            // BaseUri is the api endpoint, default is https://api.powerbi.com
            powerBIclient.BaseUri = new Uri(baseUri);
 
            try
            {
                // Create body where accessLevel = View, datasetId = "" by default
                var requestParameters = new GenerateTokenRequest(accessLevel, datasetId);
 
                // Generate EmbedToken This function sends the POST message 
                //with all parameters and returns the token
                EmbedToken token = await powerBIclient.Reports.GenerateTokenInGroupAsync(
                    groupId, 
                    reportId, 
                    requestParameters);
 
                return token.Token;
            }
            catch (Exception exc)
            {
                return exc.ToString();
            }
        }

More questions? Try asking the Power BI Community

Show: