首页 > 解决方案 > Azure HTTP POST 请求来自 Power BI 的访问令牌

问题描述

是否可以通过 HTTP POST 请求从 Power BI 电源查询中获取 Azure AD 应用程序访问令牌?

HTTP POST 请求:

//request url
https://login.microsoftonline.com/<tenant id>/oauth2/token

//header
Content-Type: application/x-www-form-urlencoded

//request body
grant_type=client_credentials
client_id=625bc9f6xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
client_secret=bCBxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
resource=api://xxxxxxxxxxxxxxxxxxxxxxxxxxx

参考:https ://docs.microsoft.com/en-us/azure/active-directory/azuread-dev/v1-oauth2-client-creds-grant-flow#example

好的,它看起来很简单,它可以很好地与Postmanorpython请求配合使用,并显示带有 json 数据的成功响应。Power BI power query现在我的问题是我想通过这个请求power query

let
  apiUrl = "https://login.microsoftonline.com/<tenant id>/oauth2/token",
    body = "{
    ""grant_type"": ""client_credentials"",
    ""client_id"": ""625bc9f6xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"",
    ""client_secret"": ""bCBxxxxxxxxxxxxxxxxxxxxxxxxxxx"",
    ""resource"": ""api://xxxxxxxxxxxxxxxxxxxxxxxxxxx""
    }",

  Source = Web.Contents(apiUrl, [Headers=[#"Content-Type"="application/x-www-form-urlencoded"],
           Content=Text.ToBinary(body)]),
  FormatAsJson = Json.Document(Source)
in
    FormatAsJson

但它说错误的请求

DataSource.Error: Web.Contents failed to get contents from 
'https://login.microsoftonline.com/61ed5503-xxxxxxxxxxxxxxxxx/oauth2/token' (400): Bad Request

我错过了什么或为什么它说错误的请求?是不是它的原因REST API?还是有其他方法可以完成它?

标签: azure-active-directoryhttp-postpowerbipowerquery

解决方案


我解决了,实际上我必须添加一些额外的术语,而且我自己也犯了一个愚蠢的错误,正确的查询

let
  apiUrl = "https://login.windows.net/61xxxxxxxxxxxx/oauth2/token",
    body = [
          client_id="3728xxxxxxxxxxxxxx5",
          grant_type="client_credentials",
          client_secret="bxxxxxxxxxxxxh",
          resource="api://xxxxxxxxxxxxxxxx5"
],

  Source = Json.Document(Web.Contents(apiUrl, [Headers = [Accept = "application/json"],
 Content = Text.ToBinary(Uri.BuildQueryString(body))]))
in
Source

推荐阅读