繁体   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