Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript和PHP自动生成嵌入令牌?_Javascript_Php_Powerbi_Token_Powerbi Embedded - Fatal编程技术网

如何使用javascript和PHP自动生成嵌入令牌?

如何使用javascript和PHP自动生成嵌入令牌?,javascript,php,powerbi,token,powerbi-embedded,Javascript,Php,Powerbi,Token,Powerbi Embedded,我也在PowerBI社区上发布了这篇文章,但没有得到任何关注: 我的报告在测试中使用了使用Microsoft Embed token-Generate token(此处)和PowerShell命令生成的令牌 我将格式设置得恰到好处,更改了一些配置,并在localhost上实现了我想要的效果 为了解决这个问题,我还通过powerbi.com(此处)使用了自动嵌入式设置。我玩了一下下载的vs文件,这表明动态生成令牌是可能的,但它都是ASP.net和C#的,我不知道如何转换它 现在,我正在尝试将它部署

我也在PowerBI社区上发布了这篇文章,但没有得到任何关注:

我的报告在测试中使用了使用Microsoft Embed token-Generate token(此处)和PowerShell命令生成的令牌

我将格式设置得恰到好处,更改了一些配置,并在localhost上实现了我想要的效果

为了解决这个问题,我还通过powerbi.com(此处)使用了自动嵌入式设置。我玩了一下下载的vs文件,这表明动态生成令牌是可能的,但它都是ASP.net和C#的,我不知道如何转换它

现在,我正在尝试将它部署到我的使用PHP和javascript的站点的生产环境中

是否有人提供了一些示例或任何我可以交换ReportId、GroupId等的内容?脚本儿童风格

以下是我正在使用的功能,除了即将到期的手动生成的令牌外,它工作得非常好:

<script src="./dist/powerbi.js"></script>
<div id="reportContainer" style="height: 1400px; width: 1000px;"></div>

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

      // Embed configuration used to describe 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 embedConfiguration = {
          type: 'report',
          tokenType: models.TokenType.Embed,
          accessToken: '<manually generated token here>',
          embedUrl: 'https://app.powerbi.com/reportEmbed',
          id: '<report id here>',
          permissions: models.Permissions.Read,
          settings: {
              // filterPaneEnabled: false,
              // navContentPaneEnabled: true,
              background: models.BackgroundType.Transparent,
              panes:{
                bookmarks: {
                  visible: false
                },
                fields: {
                  expanded: false
                },
                filters: {
                  expanded: false,
                  visible: false
                },
                pageNavigation: {
                  visible: true
                },
                selection: {
                  visible: false
                },
                syncSlicers: {
                  visible: false
                },
                visualizations: {
                  expanded: false
                }
              }

          }
      };

      // 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, embedConfiguration);
  </script>

//买模型。模型包含可以使用的枚举。
var模型=窗口['powerbi-client']。模型;
//嵌入配置用于描述嵌入的内容和方式。
//调用powerbi.embed时使用此对象。
//这还包括设置和选项,如过滤器。
//您可以在以下网址找到更多信息:https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
变量配置={
键入:“报告”,
令牌类型:models.tokenType.Embed,
accessToken:“”,
嵌入URL:'https://app.powerbi.com/reportEmbed',
id:“”,
权限:models.permissions.Read,
设置:{
//filterPaneEnabled:false,
//navContentPaneEnabled:正确,
背景:models.BackgroundType.Transparent,
窗格:{
书签:{
可见:假
},
字段:{
扩展:false
},
过滤器:{
扩展:错,
可见:假
},
页面导航:{
可见:正确
},
选择:{
可见:假
},
同步切片器:{
可见:假
},
可视化:{
扩展:false
}
}
}
};
//获取对嵌入报表HTML元素的引用
var$reportContainer=$('#reportContainer')[0];
//嵌入报表并将其显示在div容器中。
var report=powerbi.embed($reportContainer,embeddeconfiguration);

您需要对和进行两次cURL调用,以生成嵌入访问令牌。需要将YourGroupID更改为要从中嵌入的工作空间ID。您还需要将
clientid
username
password
设置为适当的值。下面的代码将为您动态设置
$embedUrl
$embed
变量。关于如何设置
ReportId
EmbedUrl
还有一些改进的余地,但这足以让嵌入工作起来

/* Get oauth2 token using a POST request */

$curlPostToken = curl_init();

curl_setopt_array($curlPostToken, array(

CURLOPT_URL => "https://login.windows.net/common/oauth2/token",

CURLOPT_RETURNTRANSFER => true,

CURLOPT_ENCODING => "",

CURLOPT_MAXREDIRS => 10,

CURLOPT_TIMEOUT => 30,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => "POST",

CURLOPT_POSTFIELDS => array(

grant_type => 'password',

scope => 'openid',

resource => 'https://analysis.windows.net/powerbi/api',

client_id => '', // Registered App ApplicationID

username => '', // for example john.doe@yourdomain.com

password => '' // Azure password for above user

)

));

$tokenResponse = curl_exec($curlPostToken);

$tokenError = curl_error($curlPostToken);

curl_close($curlPostToken);

// decode result, and store the access_token in $embeddedToken variable:

$tokenResult = json_decode($tokenResponse, true);

$token = $tokenResult["access_token"];

$embeddedToken = "Bearer "  . ' ' .  $token;

/*      Use the token to get an embedded URL using a GET request */

$curlGetUrl = curl_init();

 

curl_setopt_array($curlGetUrl, array(

CURLOPT_URL => "https://api.powerbi.com/v1.0/myorg/groups/YourGroupID/reports/",

CURLOPT_RETURNTRANSFER => true,

CURLOPT_ENCODING => "",

CURLOPT_MAXREDIRS => 10,

CURLOPT_TIMEOUT => 30,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => "GET",

CURLOPT_HTTPHEADER => array(

"Authorization: $embeddedToken",

"Cache-Control: no-cache",

),

));

$embedResponse = curl_exec($curlGetUrl);

$embedError = curl_error($curlGetUrl);

curl_close($$curlGetUrl);

if ($embedError) {

echo "cURL Error #:" . $embedError;

} else {

$embedResponse = json_decode($embedResponse, true);

$embedUrl = $embedResponse['value'][0]['embedUrl']; // this is just taking the first value. you need logic to find the report you actually want to embed. This EmbedUrl needs to match the corresponding ReportId you later use in the JavaScript.

}
现在,在执行嵌入的javascript中,您可以传入ReportID、
$embedUrl
$token
以成功嵌入报表

script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

<script src="scripts/powerbi.js"></script>

<div id="reportContainer"></div>

 

<script>

// 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 embedConfiguration= {

type: 'report',

id: 'YourReportId', // the report ID

embedUrl: "<?php echo $embedUrl ?>",

accessToken: "<?php echo $token; ?>" ,

};

var $reportContainer = $('#reportContainer');

var report = powerbi.embed($reportContainer.get(0), embedConfiguration);

</script>
scriptsrc=”http://code.jquery.com/jquery-1.11.3.min.js">
//买模型。模型包含可以使用的枚举。
var模型=窗口['powerbi-client']。模型;
//嵌入配置用于描述嵌入的内容和方式。
//调用powerbi.embed时使用此对象。
//这还包括设置和选项,如过滤器。
//您可以在以下网址找到更多信息:https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
变量配置={
键入:“报告”,
id:'YourReportId',//报告id
嵌入URL:“”,
accessToken:“”,
};
var$reportContainer=$(“#reportContainer”);
var report=powerbi.embed($reportContainer.get(0),embedConfiguration);

感谢@vvv4d让我走上了正确的轨道。对于其他人,下面是我使用的格式的工作代码。在将5个值更改为适用于您的报告的值后,它应该主要是即插即用的

请注意,我无法让它在localhost上工作,因为错误似乎与localhost和PowerBI服务器之间的身份验证问题有关。它在现场工作

让它成为你自己的吧。希望它能在所需更改非常少的情况下工作

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="./dist/powerbi.js"></script>

<div id="reportContainer" style="height: 1400px; width: 1000px;"></div>

<?php
    // All the values used below can be generated at https://app.powerbi.com/embedsetup/appownsdata
    
    /* Get oauth2 token using a POST request */
    $curlPostToken = curl_init();
    curl_setopt_array($curlPostToken, array(
        CURLOPT_URL => "https://login.windows.net/common/oauth2/token",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => array(
            grant_type => 'password',
            scope => 'openid',
            resource => 'https://analysis.windows.net/powerbi/api',
            
            // Make changes Start
            client_id => '#####################', // Registered App Application ID
            username => 'john.doe@yourdomain.com', // for example john.doe@yourdomain.com
            password => '#####################', // Azure password for above user
            // Make changes End
        )
    ));

    $tokenResponse = curl_exec($curlPostToken);
    $tokenError = curl_error($curlPostToken);
    curl_close($curlPostToken);

    // decode result, and store the access_token in $embeddedToken variable:
    $tokenResult = json_decode($tokenResponse, true);
    $token = $tokenResult["access_token"];
    $embeddedToken = "Bearer "  . ' ' .  $token;

    /* Use the token to get an embedded URL using a GET request */
    $curlGetUrl = curl_init();
    curl_setopt_array($curlGetUrl, array(
        
        // Make changes Start
        CURLOPT_URL => "https://api.powerbi.com/v1.0/myorg/groups/#####################/reports/", // Enter your Workspace ID, aka Group ID
        // Make changes End
        
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            "Authorization: $embeddedToken",
            "Cache-Control: no-cache",
        ),
    ));

    $embedResponse = curl_exec($curlGetUrl);
    $embedError = curl_error($curlGetUrl);
    curl_close($$curlGetUrl);
    if ($embedError) {
        echo "cURL Error #:" . $embedError;
        } else {
            $embedResponse = json_decode($embedResponse, true);
            $embedUrl = $embedResponse['value'][0]['embedUrl']; // this is just taking the first value. you need logic to find the report you actually want to embed. This EmbedUrl needs to match the corresponding ReportId you later use in the JavaScript.
        }
?>



<script>
    // 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 embedConfiguration= {
        type: 'report',
        
        // Make changes Start
        id: '#####################', // the report ID
        // Make changes End
        
        embedUrl: "<?php echo $embedUrl ?>",
        accessToken: "<?php echo $token; ?>",
        permissions: models.Permissions.Read,
        settings: {
            background: models.BackgroundType.Transparent,
            panes:{
                bookmarks: {
                    visible: false
                },
                fields: {
                    expanded: false
                },
                filters: {
                    expanded: false,
                    visible: false
                },
                pageNavigation: {
                    visible: false
                },
                selection: {
                    visible: false
                },
                syncSlicers: {
                    visible: false
                },
                visualizations: {
                    expanded: false
                }
            }
        }
    };

    var $reportContainer = $('#reportContainer');
    var report = powerbi.embed($reportContainer.get(0), embedConfiguration);

</script>

//买模型。模型包含可以使用的枚举。
var模型=窗口['powerbi-client']。模型;
//嵌入配置用于描述嵌入的内容和方式。
//调用powerbi.embed时使用此对象。
//这还包括设置和选项,如过滤器。
//您可以在以下网址找到更多信息:https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
变量配置={
键入:“报告”,
//开始进行更改
id:“‘#####################‘//报告id
//结束更改
嵌入URL:“”,
accessToken:“”,
权限:models.permissions.Read,
设置:{
背景:models.BackgroundType.Transparent,
窗格:{
书签:{
可见:假
},
字段:{
扩展:false
},
过滤器:{
扩展:错,
可见:假
},
页面导航:{
可见:假
},
选择:{
可见:假
},