【问题标题】:Private REST API connection Power BI (M - Power Query)私有 REST API 连接 Power BI (M - Power Query)
【发布时间】:2021-06-13 00:36:35
【问题描述】:

我想将 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”

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

我希望你能帮助我!提前致谢。

【问题讨论】:

  • 您是否尝试过在没有#"Authorization"="Basic Auth" 的情况下进行 API 调用?您是否在Postman 或类似中尝试过相同的操作?
  • 嗨安吉洛,在邮递员中它工作正常。我也试过了:#"Authorization"="Basic"。
  • 没有那个参数?您收到什么样的状态码错误?
  • 同样的错误。 "DataSource.Error: Há suporte para Web.Contents com a opção Content somente durante uma conexão anônima。详细信息:DataSourceKind=Web DataSourcePath=openid-provider.crearesistemas.com.br/auth/v1/token"
  • 很难说,Power BI 在这些错误中的描述性不是很强。您收到什么样的状态码? 401, 500?您可以使用 Wireshark 之类的内容来查看您的查询有什么问题。

标签: powerbi powerquery bearer-token rest m


【解决方案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"
    }

电源查询

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

查看更多:

【讨论】:

  • 嗨ninMOnkey,在这种情况下的端点是:api.crearecloud.com.br/frotalog/basic-services/v3/vehicles |授权:承载令牌。 |当我第一次发布这个时,我试图首先在 powerbi 中生成不记名令牌,以便使用 get 方法。这是我第一次使用 API。
  • 现在,我已尝试使用您的代码,并显示与我之前遇到的几乎相同的错误。它说我不能使用授权作为使用 Web 的标头。内容。
【解决方案2】:

这就是我在他们为其创建 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"

【讨论】:

  • + @Bruno Fantinelli 如果您使用 web.contents 参数 ApiKeyName 并将其设置为 API 期望的值,则 GUI 将允许您将凭据存储在凭据存储中,而不是查询本身。如果您没有看到它,则靠近连接属性和隐私。如果您选择匿名,那么您最终会在查询中获得凭据 - 或报告参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 2022-10-05
  • 1970-01-01
相关资源
最近更新 更多