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 a

KB 294807 - IIS Server - PHP FastCGI 500 Error 0xfffffffe

Resolved issue today of IIS Server - PHP FastCGI 500 Error 0xfffffffe by deleting "Windows Temp" folder files. We used Cygwin to customize some permissions of "Windows Temp" folder and after few hours the website stopped working. The search online was giving all kinds of different solution except what I was looking for. I was hoping the stackoverflow will have answers but even this post didn't cover the issue I was running into. https://stackoverflow.com/questions/15302080/fastcgi-500-failure Restarting the IIS didn't work, but deleting temp files cleared the issue, but now the problem is how long before the site will go down again? Hope the issue is resolved for ever, if not; I will update this post later.

QlikSense - Custom Form Login Screen

Extension of:  Custom login, logout and error forms for Qlik Sense by RPK https://developer.qlik.com/garden/5ada5a8f0c313f5c539dc7fd QlikSense - Custom Login Screen Install Node on QlikSense Server: URL: https://nodejs.org/en/ Saved at: C:\server-tools\Downloads\Node Installed at: C:\server-tools\Runtime\nodejs\ Keep default options Create Folder: C:\QlikForm Clone git Repository and keep updates private: # Open Shell/Bash/Command Prompt cd /QlikForm # Ref: https://medium.com/@bilalbayasut/github-how-to-make-a-fork-of-public-repository-private-6ee8cacaf9d3 git clone --bare https://github.com/kreta99/QS-Custom-Forms .git cd QS-Custom-Forms.git git push --mirror https://github.com/<name>/QS-Custom-Forms.git cd .. (go to parent folder) # remove other users repo clone rm -rf QS-Custom-Forms.git # clone witinc private repo git clone https://github.com/<name>/QS-Custom-Forms.git Get latest copy of login pages: # Open She