如何将入站请求正文传递到 Azure API 网关中的后端服务
How to pass the inbound request body to the backend service in Azure API Gateway
我正在尝试通过 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/ 所以我修改了我的 inboud 政策如下但仍然没有运气。
<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" />
任何潜在客户都将得到批准。
在 inbound
属性下设置 <set-backend-service base-url="https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token" />
。
这里有一个类似的 你可以参考。
知道了,从入站策略中删除了标题和方法,并将入站策略更新为:-
<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" />
我正在尝试通过 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/ 所以我修改了我的 inboud 政策如下但仍然没有运气。
<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" />
任何潜在客户都将得到批准。
在 inbound
属性下设置 <set-backend-service base-url="https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token" />
。
这里有一个类似的
知道了,从入站策略中删除了标题和方法,并将入站策略更新为:-
<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" />