Azure APIM 中的动态命名值

问题描述 投票:0回答:1

我正在尝试通过我的逻辑应用程序将“NAMED VALUE”名称传递给 APIM。上下文是,根据调用合作伙伴,我需要为 Salesforce 使用特定的凭据。请参阅下面的 APIM 政策片段。

<!-- START Dynamic SF user identification -->
<set-variable name="nv_username" value="@(context.Request.Headers.GetValueOrDefault("nv_username"))" />
<set-variable name="nv_password" value="@(context.Request.Headers.GetValueOrDefault("nv_password"))" />
<choose>
    <when condition="@(context.Request.Headers.ContainsKey("nv_username"))">
        <set-variable name="get_tokenbody" value="@("grant_type=password&client_id={{Common-Salesforce-ClientID}}&client_secret={{Common-Salesforce-ClientSecret}}&username={{" + (string)context.Variables["nv_username"] + "}}&password={{" + (string)context.Variables["nv_password"] + "}}")" />
    </when>
    <otherwise>
        <set-variable name="get_tokenbody" value="@("grant_type=password&client_id={{Common-Salesforce-ClientID}}&client_secret={{Common-Salesforce-ClientSecret}}&username={{Common-Salesforce-Username}}&password={{Common-Salesforce-Password}}")" />
    </otherwise>
</choose>
<!-- END Dynamic SF user identification -->

但是,当策略运行时,它不会用实际值替换命名值。例如,参考下面的 APIM Trace

set-variable (0.001 ms)
{
    "message": "Context variable was successfully set.",
    "name": "get_tokenbody",
    "value": "grant_type=password&client_id=<redacted>&client_secret=<redacted>&username={{Project1-Salesforce-Username}}&password={{Project1-Salesforce-Password}}"
}

你能建议我如何让 APIM 获得实际的 NamedValues 吗?

azure-api-management
1个回答
0
投票

我猜你只是想摆脱

{{
.
因此,我刚刚删除了
{{
}}

&username={{" + (string)context.Variables["nv_username"] + "}}&password={{" + (string)context.Variables["nv_password"] + "}}")" />

以下策略返回字符串,其值存储为命名值或传递的标头值:

<policies>
    <inbound>
        <base />
        <!-- START Dynamic SF user identification -->
        <set-variable name="nv_username" value="@(context.Request.Headers.GetValueOrDefault("nv_username"))" />
        <set-variable name="nv_password" value="@(context.Request.Headers.GetValueOrDefault("nv_password"))" />
        <choose>
            <when condition="@(context.Request.Headers.ContainsKey("nv_username"))">
                <set-variable name="get_tokenbody" value="@("grant_type=password&client_id={{Common-Salesforce-ClientID}}&client_secret={{Common-Salesforce-ClientSecret}}&username=" + (string)context.Variables["nv_username"] + "&password=" + (string)context.Variables["nv_password"])" />
            </when>
            <otherwise>
                <set-variable name="get_tokenbody" value="@("grant_type=password&client_id={{Common-Salesforce-ClientID}}&client_secret={{Common-Salesforce-ClientSecret}}&username={{Common-Salesforce-Username}}&password={{Common-Salesforce-Password}}")" />
            </otherwise>
        </choose>
        <!-- END Dynamic SF user identification -->
        <return-response>
            <set-status code="200" reason="OK" />
            <set-header name="Content-Type" exists-action="override">
                <value>application/json</value>
            </set-header>
            <set-body>@{   
                var response = new JObject();
                response["tokenbody"] = context.Variables.GetValueOrDefault<string>("get_tokenbody");
                return response.ToString();
            }</set-body>
        </return-response>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

带有标头值的请求:

POST https://rfqapiservicey27itmeb4cf7q.azure-api.net/sample/clone-64182/ipsum HTTP/1.1
主机:rfqapiservicey27itmeb4cf7q.azure-api.net
nv_username:用户名
nv_password:密码

带有标题中值的响应正文:

{
    "tokenbody": "grant_type=password&client_id=lorem&client_secret=secret&username=theusername&password=thepassword"
}

带命名值的请求

配置的命名值:

POST https://rfqapiservicey27itmeb4cf7q.azure-api.net/sample/clone-64182/ipsum HTTP/1.1
主机:rfqapiservicey27itmeb4cf7q.azure-api.net

{
    "tokenbody": "grant_type=password&client_id=lorem&client_secret=secret&username=ipsum&password=pw"
}

© www.soinside.com 2019 - 2024. All rights reserved.