【问题标题】:How to pass the inbound request body to the backend service in Azure API Gateway如何将入站请求正文传递到 Azure API 网关中的后端服务
【发布时间】:2020-01-30 07:10:12
【问题描述】:

我正在尝试通过 Azure API 网关生成 Active Directory 令牌。为此,我在我的 Azure API 网关上创建了一个 API 操作,它接受以下主体参数。

  {
     "client_id" :"****************",
     "scope":"https://graph.windows.net/.default",
     "client_secret":"****************",
     "grant_type":"client_credentials"  
  }

每当我尝试对此进行测试时,都会为入站进程设置正文,但无法将其转发到后端服务,即 https://login.microsoftonline.com/{{ID}}/oauth2/v2.0/token/ 所以我修改了我的入境政策如下,但仍然没有运气。

        <set-method>POST</set-method>
    <set-variable name="requestBodyData" value="@(context.Request.Body.As<string>(preserveContent: true))" />
    <set-header name="Content-Type" exists-action="override">
        <value>"application/x-www-form-urlencoded"</value>
    </set-header>
    <rewrite-uri template="/" />
    <set-body>@{
          return "client_id=*******&scope=https://graph.windows.net/.default&client_secret=*******&grant_type=client_credentials";
      }</set-body>
    <!--  Don't expose APIM subscription key to the backend. -->
    <set-header name="Ocp-Apim-Subscription-Key" exists-action="delete" /> 

任何潜在客户都会得到认可。

【问题讨论】:

    标签: azure azure-active-directory azure-api-management api-management


    【解决方案1】:

    inbound属性下设置&lt;set-backend-service base-url="https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token" /&gt;

    这里有一个类似的issue,你可以参考一下。

    【讨论】:

    • 谢谢@joey,这似乎是一种解决方法,因为我的后端已经设置好了。会试试的。
    • 我试过了,但同样的“错误请求”响应又回来了。 ` "error": "invalid_request", "error_description": "AADSTS900144: 请求正文必须包含以下参数:'grant_type'。\r\n跟踪 ID: *************0\ r\n相关 ID:****************\r\n时间戳:********",`
    【解决方案2】:

    知道了,从入站策略中删除了标题和方法,并将入站策略更新为:-

        <set-variable name="client_id" value="********" />
        <set-variable name="scope" value="********"" />
        <set-variable name="client_secret" value="********" />
        <set-variable name="grant_type" value="********" />
        <rewrite-uri template="/" />
        <set-body>@{
            return "client_id="+(context.Variables["client_id"])+"&scope="+(context.Variables["scope"])+"&client_secret="+(context.Variables["client_secret"])+"&grant_type="+(context.Variables["grant_type"]);
            }</set-body>
        <!--  Don't expose APIM subscription key to the backend. -->
        <set-header name="Ocp-Apim-Subscription-Key" exists-action="delete" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2021-03-13
      相关资源
      最近更新 更多