簡體   English   中英

在Power Query中調用Power BI API

[英]Call Power BI API in Power Query

我正在嘗試從 Power BI API 服務檢索數據,同樣是通過 PowerShell 代碼完成的。 但不幸的是,由於某些情況,我無法將其部署到生產環境中。 因此,現在我正嘗試在 Power BI 桌面本身中完成同樣的事情,以便我只能從 Power Query 調用 power BI rest API。 有大量關於在 power 查詢中調用 API 的博文,但它們都需要 Power BI App 注冊客戶端 ID。 我沒有。 我成功地能夠在 PowerShell 中使用我的用戶名密碼進行呼叫,甚至我也收到了 API 的回復。

請在下面找到 PowerShell 代碼,如果我們可以在 Power Query 中復制相同的代碼,請告訴我。

# User credential
$User = 'shahab***@*****.com'
$Pword = ConvertTo-SecureString –String '***password***' –AsPlainText -Force

$Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User,$Pword

# Connect to service
Login-PowerBIServiceAccount -Credential $Credential  

#Get Bearer token
$headers = Get-PowerBIAccessToken  

$uri = 'https://api.powerbi.com/v1.0/myorg/datasets/f52f2abc-6445-41ee-ce02-3908c6e18dd4/refreshes' 
$refreshes = Invoke-RestMethod -Uri $uri -Headers $headers -Method GET
$xs= $refreshes

謝謝。

使用包裝器,您需要這樣的東西。

WebRequest_Simple(
    "https://api.powerbi.com",
    "v1.0/myorg/datasets/f52f2abc-6445-41ee-ce02-3908c6e18dd4/refreshes",
    [
        Headers = [ Authorization = "Bearer OAuthTokenHere" ]
    ]
)

如果 url 有一個查詢字符串( ?之后的部分),那么您將使用options[Query]

Web 回復

在此處輸入圖像描述

完整查詢

來源: WebRequest_Simple.pq

let    
    /*
    Wrapper for Web.Contents  returns response metadata 
        for options, see: <https://learn.microsoft.com/en-us/powerquery-m/web-contents#__toc360793395>

    */
    WebRequest_Simple
        =  (
            base_url as text,
            optional relative_path as nullable text,
            optional options       as nullable record
        )
        as record =>
        let 
    
            headers = options[Headers]?, //or: ?? [ Accept = "application/json" ],

            merged_options = [      
                Query = options[Query]?,
                RelativePath = relative_path,
                ManualStatusHandling = options[ManualStatusHandling]? ?? { 400, 404 },
                Headers = headers
            ],
        
            bytes = Web.Contents(base_url, merged_options),
            response = Binary.Buffer(bytes),
            response_metadata = Value.Metadata( bytes ),
            status_code = response_metadata[Response.Status]?,
            json = Json.Document(response),
            Final = [
                request_url = metadata[Content.Uri](),
                status_code = status_code,
                metadata = response_metadata,
                IsJson = not (try json)[HasError],
                response = response,
                json = json
            ]
        in 
            Final,

    tests = {
        WebRequest_Simple("https://httpbin.org", "json"), // expect: json
        WebRequest_Simple("https://www.google.com"),       // expect: html
        WebRequest_Simple(
            "https://api.powerbi.com",
            "v1.0/myorg/datasets/f52f2abc-6445-41ee-ce02-3908c6e18dd4/refreshes",
            [
                Headers = [ Authorization = "Bearer OAuthTokenHere" ]
            ]
        )
    },

    FinalResults = Table.FromRecords(tests,
        type table[
            status_code = text, request_url = text, metadata = record,
            response = binary, IsJson = logical, json = any],
        MissingField.Error
    )    
in
    FinalResults

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM