fmp-dev/power-bi-embedded 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fmp-dev/power-bi-embedded

Composer 安装命令:

composer require fmp-dev/power-bi-embedded

包简介

A PowerBI API Interface for PHP

README 文档

README

1. What is it?

A PHP Library for the PowerBI Rest API.

2. Which services were implemented?

Service Class
Dataset \PowerBiEmbedded\Services\Dataset
Group \PowerBiEmbedded\Services\Group

Please feel free to contribute with additional services.

3. How it works?

3.1 Create a dataset into the default workspace/group

    // Instatiate the client passing the auth token.     
    $client = new \PowerBiEmbedded\Client($token);
    
    // Create a dataset
    $dataset_model              = new \PowerBiEmbedded\Models\DatasetModel();
    $dataset_model->name        = 'My Dataset';
    $dataset_model->defaultMode = \PowerBiEmbedded\Enums\DatasetModeEnum::PUSH;
    
    // Add table to dataset
    $table = new \PowerBiEmbedded\Models\TableModel();
    $table->name = 'My Table';
    
    // Add columns to table
    $columns = [
        new \PowerBiEmbedded\Models\ColumnModel(
        [
            'name'     => 'first_column',
            'dataType' => \PowerBiEmbedded\Enums\DatatypesEnum::STRING
        ]),
        new \PowerBiEmbedded\Models\ColumnModel(
        [
            'name'     => 'second_column',
            'dataType' => \PowerBiEmbedded\Enums\DatatypesEnum::INT64
        ]),            
    ];
           
    $table->columns = $columns;
    
    // Attach tables to dataset model
    $dataset_model->tables = [$table];
    
    // Create dataset.
    // 
    // Note: Use postDatasetInGroup method in case that dataset should be created in a different
    // workspace/group. 
    $result = (new \PowerBiEmbedded\Services\Dataset($client))->postDataset($dataset_model);
    
           
    // Check if dataset was sucessfully created
    if ($result->isOk()) {
        echo "Dataset created: ";
        
        // Display dataset Id
        echo $result->response()->id;
    }

3.2 Create a workspace/group

     // Instatiate the client passing the auth token.     
     $client = new \PowerBiEmbedded\Client($token);
                      
     $result = (new \PowerBiEmbedded\Services\Group($client))->postGroup('My new workspace');
     
     if ($result->isOk()) {
         echo "Group created: ";
                 
         // Display dataset Id
         echo $result->response()->id;
     }

3.3 Add rows to dataset

    // Add rows/records
    $rows = 
    [
        [
            'first_column'  => 'foo',
            'second_column' => 1
        ],
        [
            'first_column'  => 'bar',
            'second_column' => 2
        ]
    ];
     
     
    // Post rows/records.
    // 
    // Note: Use postRowsByDatasetIdInGroup method in case that dataset is assigned to the 
    // non-default dataset.
    $result = (new Dataset(static::$client))->postRowsByDatasetId($rows, 'My Table', $dataset_id);
     
    // Obtain the results as an associative array.
    var_dump($result->response(true));

4. How to obtain the authentication token (Non-interactive authentication)

There different ways to obtain the authentication token but this way is most used:

  1. Register an Azure Active Directory App with all the required permissions

  2. Copy somewhere the "Application Id" and "Client secret"

  3. Grant admin consent to the created application (Required for non-interactive authentication)

  4. Find the tenand ID (Directory ID) and copy it to somewhere (Azure Console -> Dashboard -> Azure Active Directory -> Properties -> Directory ID)

  5. Execute the following code (Note: league/oauth2-client library is required):

     $application_id     = '<The application id>';
     $application_secret = '<The application secret>';
     
     $directory_id       = '<The azure tenant id>';
     
     $user               = '<Admin e-mail or username used in the app registration process>';
     $password           = '<Admin password used in the app registration process>';
     
     $token_file         = 'token.txt';
    
     // Authenticate
     $provider = new \League\OAuth2\Client\Provider\GenericProvider([
     	'clientId'                => $application_id,
     	'clientSecret'            => $application_secret,
     	'urlAuthorize'            => "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
     	'urlAccessToken'          => "https://login.windows.net/$directory_id/oauth2/token",
     	'urlResourceOwnerDetails' => '',
     	'scopes'                  => 'openid',
     ]);
    
     try {
     	// Try to get an access token using the resource owner password credentials grant.
     	$accessToken = $provider->getAccessToken('password', [
     		'username' => $user,
     		'password' => $password,
     		'resource' => 'https://analysis.windows.net/powerbi/api'
     	]);
    
     	$token = $accessToken->getToken();
    
     } catch (\Exception $e) {
         
     	echo 'Unable to retrieve token' . PHP_EOL;
     	
     	die($e->getMessage());		
     }
    
     // Save token
     file_put_contents($token_file, $token);
    
     echo '🔑 Token saved in ' . $token_file;
    

5. PowerBI API Restrictions

This library doesn't take care about the PowerBI API Restrictions.

6. Unit tests

Unit tests are not mocked, so it means that request are done against the real PowerBI API. Never execute the unit tests against a production account.

In order to execute the tests the auth token should be added to the environment variable "POWERBI_AUTH_TOKEN":

    export POWERBI_AUTH_TOKEN=<Your token>

"# power-bi-embedded"

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固