fmp-dev / power-bi-embedded
PHP的PowerBI API接口
dev-main
2022-08-13 07:12 UTC
Requires
- guzzlehttp/guzzle: ^6.3|^7.0.1
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is not auto-updated.
Last update: 2024-09-21 17:31:37 UTC
README
1. 这是啥?
PHP库,用于PowerBI Rest API。
2. 哪些服务已被实现?
请随时贡献额外的服务。
3. 它是如何工作的?
3.1 在默认的工作空间/组中创建数据集
// 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 创建工作空间/组
// 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/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. 如何获取认证令牌(非交互式认证)
有多种方法可以获取认证令牌,但以下方法最常用
-
注册一个具有所有所需权限的Azure Active Directory 应用
-
将“应用程序 ID”和“客户端密钥”复制到某处
-
为创建的应用程序授予管理员权限(非交互式认证所需)
-
找到租户 ID(目录 ID)并将其复制到某处(Azure 控制台 -> 仪表板 -> Azure Active Directory -> 属性 -> 目录 ID)
-
执行以下代码(注意:需要 league/oauth2-client 库)
$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 限制
此库不处理PowerBI API 限制。
6. 单元测试
单元测试不模拟,这意味着请求是针对真实的 PowerBI API 进行的。请勿在生产帐户上执行单元测试。
为了执行测试,应将认证令牌添加到环境变量“POWERBI_AUTH_TOKEN”中
export POWERBI_AUTH_TOKEN=<Your token>
"# power-bi-embedded"