私有 REST API 连接 Power BI (M - Power Query)

时间:2021-03-15 18:00:02

标签: powerbi powerquery bearer-token restapi m

我想将 powerbi 与客户端私有 Rest API 连接。所以,我一直在尝试用 M 编写代码以便做到这一点,但我不是 M 语言的专家。

这就是我尝试在 Power bi 中生成不记名令牌的方法。你能帮我写代码吗?

  let
    url = "https://openid-provider.crearesistemas.com.br/auth/v1/token?lang=pt-BR",
    body  = "{ ""client_id"": ""*****"",  ""client_secret"": ""*****"", ""grant_type"": ""client_credentials""}",

    tokenResponse = Json.Document(Web.Contents(url,
    [Headers = [#"Authorization"="Basic Auth", #"Content-Type"="application/json"
    ],
     Content = Text.ToBinary(body)
    ] 
    )),
    AccessToken = tokenResponse[access_token],
    AccessTokenHeader = "Bearer " & AccessToken
in 
AccessTokenHeader 

我不知道我做错了什么。 它显示以下错误:“DataSource.Error”“DataSourceKind=Web”“DataSourcePath=https://openid-provider.crearesistemas.com.br/auth/v1/token”

生成 Bearer Token 后,我必须使用“get 方法”才能从 api 请求客户端数据。
main_api_url: https://api.crearecloud.com.br/frotalog/basic-services/v3/ ... 授权:Bearer Token json格式

希望大家帮帮我!提前致谢。

2 个答案:

答案 0 :(得分:1)

如果您将文档链接到您尝试使用的端点会有所帮助,否则我们不确切知道您需要什么。您的目标是什么 HTTP 请求?

<块引用>

我必须使用'get方法'

对于 GET 请求,您使用 Query,对于 POST,您使用 Content。都接受type record

输入

对于第 1 步,我认为您将这些参数作为 GET 请求请求。 如果您需要 POST,只需将 Query 替换为 Content

using the example 
    "GET" request and:
    "https://openid-provider.crearesistemas.com.br/auth/v1/token?lang=pt-BR",
    {
        "client_id": "*****", 
        "client_secret": "*****",
        "grant_type": "client_credentials"
    }

Power Query

let

    Headers = [
        Accept="application/json",
        Authorization = "Basic Auth"
    ],
    BaseUrl = "https://openid-provider.crearesistemas.com.br",
    Options = [
        RelativePath = "/auth/v1/token",
        Headers = Headers,
        Query = [
            lang = "pt-BR",
            client_id = "id",
            client_secret = "secret",
            grant_type = "client_credentails"
        ],
        ManualStatusHandling = {400, 404}
    ],
    // wrap 'Response' in 'Binary.Buffer' if you are referencing it multiple times
    response_binary = Web.Contents(BaseUrl, Options),
    buffered = Binary.Buffer(response_binary),
    response_metadata = Value.Metadata(response_binary),
    status_code = response_metadata[Response.Status],

    // for 400 and 404, return extra error info
    maybe_bytes =
        if List.Contains({400, 404}, status_code)
        then response_metadata else buffered,
    from_json = Json.Document(maybe_bytes)
    // returns either document else web request error
in
    from_json

查看更多:

答案 1 :(得分:0)

这就是我在为它制作 API 之前获取数据流刷新数据的方式。理想情况下,人们不会在查询中输入凭据。

let
    APIString =" https://api.powerbi.com/v1.0/myorg/gr...​",
    GetToken = 
    let
        client_id = yourclientID,
        client_secret= yourclientsecret,
        redirect_uri = "http://localhost:13526/redirect",
        token_uri = "https://login.windows.net/common/oauth2/token", 
        authorize_uri = "https://login.windows.net/common/oauth2/authorize",
        resource="https://analysis.windows.net/powerbi/api",
        username=yourusername,
        password=yourpassword,
        tokenResponse = Json.Document(Web.Contents(token_uri, [Content = Text.ToBinary(Uri.BuildQueryString( [
                                            client_id = client_id
                                            ,client_secret=client_secret
                                            ,username=username
                                            ,password=password
                                            ,resource=resource
                                            ,grant_type = "password"
                                            ,redirect_uri = redirect_uri ])), Headers= [Accept="application/json"]
                                            ,ManualStatusHandling= {400} ])),
        access_token = tokenResponse[access_token],
        token = tokenResponse[access_token]
    in
        token,
    GetGroups = Json.Document(Web.Contents(APIString, [ Headers = [ Authorization="Bearer "& GetToken ], ManualStatusHandling={400} ] )),
    value = GetGroups[value],
    #"Converted to Table" = Table.FromList(value, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "refreshType", "startTime", "endTime", "status"}, {"id", "refreshType", "startTime", "endTime", "status"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1",{{"id", type text}, {"refreshType", type text}, {"status", type text}, {"startTime", type datetimezone}, {"endTime", type datetimezone}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"endTime", type datetime}, {"startTime", type datetime}})
in
    #"Changed Type1"