Skip to main content

Embed Dashboard to External Application Using JWT

Install PHP dependency

composer require firebase/php-jwt

 

For Yii2 Framework

Controller

use Firebase\JWT\JWT;

// Tableau Server configuration
$tableauServer = "https://YOUR_TABLEAU_SERVER";
$clientId = "YOUR_CLIENT_ID"; // From the Connected App
$secretId = "YOUR_SECRET_ID"; // From the Connected App
$secretValue = "YOUR_SECRET_VALUE"; // From the Connected App

// JWT payload
$issuedAt = time();
$expirationTime = $issuedAt + 300; // JWT valid for 5 minutes
$payload = [
    "iss" => $clientId, // Client ID from Connected App
    "exp" => $expirationTime,
    "jti" => Yii::$app->security->generateRandomString(),
    "aud" => "tableau",
    "sub" => "embed_dashboard", // Tableau username
    "scp" => ["tableau:views:embed", "tableau:metrics:embed"]
];
$header = [
    'kid' => $secretId,
    'iss' => $clientId
];

// Generate JWT using the client secret
$jwt = JWT::encode($payload, $secretValue, 'HS256', null, $header);

return $this->render('index', [
    'jwt' => $jwt
]);

View

<script type='module' src='https://evetdashboard.dvs.gov.my/javascripts/api/tableau.embedding.3.latest.min.js'></script>
<tableau-viz 
    id='tab-viz'
    src='https://evetdashboard.dvs.gov.my/views/evetpermit-premisInfografik/MenuUtamaDashboard'
    toolbar='hidden'
    token="<?php echo $jwt; ?>"
    >
</tableau-viz>