最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Code to execute DMV queries on Power BI Datasets without using Client ID and Secret - Stack Overflow

programmeradmin3浏览0评论

Using below code to query DMV, primary on system tables $SYSTEM.TMSCHEMA_TABLES, I dont have a client id and a secret rather I have a valid Power bi access token that has read write access to the Power BI workspace and the Dataset, how can I make below code work and remove dependency on client id and secret, I am open for an equivalent solution in pyspark as well (provided it works :) )

using Microsoft.AnalysisServices.AdomdClient;
using Azure.Identity;
using Azure.Core;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // Configuration
        string xmlaEndpoint = "powerbi://api.powerbi/v1.0/myorg/<workspace-name>";
        string tenantId = "<your-tenant-id>";
        string clientId = "<your-client-id>";
        string clientSecret = "<your-client-secret>";
        string scope = "/.default";

        try
        {
            // Step 1: Acquire access token using ClientSecretCredential (Service Principal)
            Console.WriteLine("Acquiring access token with ClientSecretCredential...");

            var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
            var tokenRequestContext = new TokenRequestContext(new[] { scope });
            var accessToken = (await credential.GetTokenAsync(tokenRequestContext)).Token;

            Console.WriteLine("Access token acquired successfully.");
            
            // Step 2: Create connection string (without token in connection string)
            string connectionString = $"Data Source={xmlaEndpoint};";

            // Step 3: Open ADOMD connection
            using (AdomdConnection connection = new AdomdConnection(connectionString))
            {
                // Apply the access token manually using SessionID
                connection.SessionID = accessToken;

                Console.WriteLine("Opening connection to XMLA endpoint...");
                connection.Open();

                // Step 4: Execute DAX Query
                string query = "SELECT [Name] FROM $SYSTEM.TMSCHEMA_TABLES";

                using (AdomdCommand command = new AdomdCommand(query, connection))
                {
                    Console.WriteLine("Executing query...");
                    using (AdomdDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Console.WriteLine(reader[0]);  // Print first column (adjust as needed)
                        }
                    }
                }

                connection.Close();
                Console.WriteLine("Connection closed successfully.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}

Note: I have already tried:

POST .0/myorg/datasets/{datasetId}/executeQueries

This works only for DAX queries, I want to query System tables in this case like - $SYSTEM.TMSCHEMA_TABLES

发布评论

评论列表(0)

  1. 暂无评论