Skip to main content

PowerBI Enterprise App Embed Solution in PHP

I had a nightmare time trying to figure out a simple example for JavaScript API, for PowerBI embed solution using PHP programming language.

I hope developers post every single step solutions when possible as it's very nightmarish to figure out that which we don't know.

I hope following notes could be useful to some, and if I myself have forgotten to include any steps or the steps are no longer valid due to upgrades or software changes, please post it in comments and I will try to update the steps if possible or create a new blog and add a link to it in this blog post.




Power BI

Azure Portal

URL: https://portal.azure.com


Setting up an Embed App
Embed Onboarding
URL: https://app.powerbi.com/embedsetup/appownsdata

  1. Sign In
    1. “your-org-email-org-office-365-email”
  2. Register your application
    1. Application Name: Power BI Integration
    2. API access
      1. Read only APIs (check items below)
        1. Read all datasets
        2. Read all dashboards
        3. Read all reports
        4. Read all groups
        5. Read all workspaces
        6. Read all capacities
    3. Register (click button)
    4. Record:
      1. Application ID: {GUID}
  3. Create a workspace
    1. Name your workspace: DWC Public Workspace
    2. Create app workspace (click button)
    3. Record:
      1. Workspace ID: {GUID}
  4. Import content
    1. Upload .pbix file (check)
      1. Browse (click link)
      2. File: Select from computer
    2. Import (click button)
    3. Record:
      1. Report ID: : {GUID}
  5. Grant permissions
    1. Popup
      1. Select the checkbox for delegating permissions as organization.


Endpoints

Home > WIT - App registrations > Endpoints



FEDERATION METADATA DOCUMENT
https://login.microsoftonline.com/{GUID}/federationmetadata/2007-06/federationmetadata.xml

WS-FEDERATION SIGN-ON ENDPOINT
https://login.microsoftonline.com/{GUID}/wsfed

SAML-P SIGN-ON ENDPOINT
https://login.microsoftonline.com/{GUID}/saml2

SAML-P SIGN-OUT ENDPOINT
https://login.microsoftonline.com/{GUID}/saml2

MICROSOFT AZURE AD GRAPH API ENDPOINT
https://graph.windows.net/{GUID}

OAUTH 2.0 TOKEN ENDPOINT
https://login.microsoftonline.com/{GUID}/oauth2/token

OAUTH 2.0 AUTHORIZATION ENDPOINT
https://login.microsoftonline.com/{GUID}/oauth2/authorize


PHP Code:

// start

$post_values = array(
    'grant_type' => 'password',
    'client_id' => '{application_id}',
    'scope' => '*',
    'resource' => 'https://analysis.windows.net/powerbi/api',
    'username' => 'web-app-email-address',
    'password' => 'web-app-password',
);

$post_url = 'https://login.microsoftonline.com/{GUID}/oauth2/token';

$response = Http::post_to_url($post_values, $post_url);
/*
 *  result items =
        token_type
        scope
        expires_in
        ext_expires_in
        not_before
        resource
        access_token
        refresh_token
*/

$embed_info = array();
if (!empty($response[0]))
{
    $result = json_decode($response[0]);
    debug::variable($result);

    $embed_info['token_type'] = $result->token_type;
    $embed_info['scope'] = $result->scope;
    $embed_info['expires_in'] = $result->expires_in;
    $embed_info['ext_expires_in'] = $result->ext_expires_in;
    $embed_info['not_before'] = $result->not_before;
    $embed_info['resource'] = $result->resource;
    $embed_info['access_token'] = $result->access_token;
    $embed_info['refresh_token'] = $result->refresh_token;

    $embed_info['embed_url'] = 'https://app.powerbi.com/reportEmbed?' .
        'reportId=' . {GUID} .
        '&groupId=' . {GUID}
    ;
    $embed_info['report_id'] = {GUID};
}

// end


User JavaScript API to implement html
URL: https://github.com/Microsoft/PowerBI-JavaScript

HTML Code:
    
    
// start

div id="reportContainer" /div

!-- libraries --
    script src="/javascript/powerbi/other/es6-promise.auto.min.js"/script
    script src="/javascript/powerbi/other/fetch.js"/script
    script src="/javascript/powerbi/powerbi.min.js"/script

    script
        // Read embed application token from Model
        var accessToken = $embed_app_info['access_token'];

        // Read embed URL from Model
        var embedUrl = $embed_app_info['embed_url'];

        // Read report Id from Model
        var embedReportId = $embed_app_info['report_id'];

        // Get models. models contains enums that can be used.
        var models = window['powerbi-client'].models;

        // Embed configuration used to describe the what and how to embed.
        // This object is used when calling powerbi.embed.
        // This also includes settings and options such as filters.
        // You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
        var config = {
    type: 'report',
    tokenType: models.TokenType.Embed,
    accessToken: accessToken,
    embedUrl: embedUrl,
    id: embedReportId,
    permissions: models.Permissions.All,
    settings: {
    filterPaneEnabled: true,
    navContentPaneEnabled: true
    }
 };

 // Get a reference to the embedded report HTML element
 var reportContainer = $('#reportContainer')[0];

 // Embed the report and display it within the div container.
 var report = powerbi.embed(reportContainer, config);
    /script

// end


Note:
In some instances you may need a "Native" app and a "Web App / API" app under "Enterprise Apps" or "App Registrations" in "Azure Portal"

Comments

Popular posts from this blog

Completed UiPath - RPA Developer Advance Training

After a month of training, with late nights and weekends I have completed the UiPath RPA Developer Advance Training.

The Level 1 Foundation Training pretty much covered all the basics of Desktop studio and workflows, only draw back was I started with the default foundation course which was for 2016, and all document reference links where pointing to 2016 version. I didn't understand it during the foundation training and kept switching to 2018 version document/page.

Only after completing the Foundation course that I realized, I was suppose to have started with the 2018 version of Level 1 Foundation Training, but as it was optional I didn't have to retake the 2018 version, the 2016 was good enough for moving to level 2.

The Level 2 Orchestrator 2018.3 Training as good. It gave good understanding of Queue's, Jobs, and schedules and I was able to complete the level two much faster then the level 1.

The Level 3 UiPath Advanced Training was bit tough as it involved the assignme…

Working hours in six months to a year

I usually try to look for how many hours I have worked on a given project and what hours that I  spend on learning or network management tasks.

Below I have added some hours calculation, after finding the breakdown by months from Berkeley website.


Credit: http://controller.berkeley.edu/payroll/payroll-system-pps/pps-training-materials/number-working-hours-month

2017Working hours six months (Jan 1st - Jun 30th): 
130 days x 8 hours =  1040 hours


Working hours six months (Jul 1st - Dec 31th): 
130 days x 8 hours =  1040 hours

Working hours for the year:
1040 hours + 1040 hours = 2080 hours
MonthWorking Days in the monthMonthly Working Hrs1/2 of working hrsMidpoint of working days in the monthDates of HolidaysJanuary22176888 a.m. 1/172,16February20160808 a.m. 2/1520March2318492Noon 3/1631April20160808 a.m. 4/17May2318492Noon 5/1629June22176888 a.m. 6/16July2116884Noon 7/174August2318492Noon 8/16September2116884Noon 9/154October22176888 a.m. 10/17November22176888 a.m. 11/1610,23,24December21168…