【问题标题】:WSO2 EI: Can I use mediator to request another API and pass its response to the body request?WSO2 EI:我可以使用调解器请求另一个 API 并将其响应传递给正文请求吗?
【发布时间】:2021-07-05 07:12:37
【问题描述】:

在我的例子中,我想在 header mediator 中添加一个动态值(“Bearer”+ {access-token})。 因此,在标头中介之前,我想调用一个 get-token API 并从其响应中提取 {access-token} 元素。我怎么能得到那个?非常感谢。

【问题讨论】:

    标签: dynamic wso2 token wso2ei mediator


    【解决方案1】:

    您可以使用中介序列来满足此类要求。您可以参考此blog,了解有关如何根据您的要求开发序列的更详细说明。该博客是为 API Manager 产品编写的,但是,您可以按照相同的方式在 EI 中完成它。

    一个示例中介序列如下

    <?xml version="1.0" encoding="UTF-8"?>
    <sequence name="oauth2-sequence" xmlns="http://ws.apache.org/ns/synapse">
        <!-- token generation to the oauth server's token endpoint -->
        <!-- add the base64 encoded credentials -->
        <property name="client-authorization-header" scope="default" type="STRING" value="MDZsZ3BTMnh0enRhOXBsaXZGUzliMnk4aEZFYTpmdE4yWTdLcnE2SWRsenBmZ1RuTVU1bkxjUFFh" />
        <property name="request-body" expression="json-eval($)" scope="default" type="STRING" />
        <property name="resource" expression="get-property('axis2', 'REST_URL_POSTFIX')" scope="default" type="STRING" />
    
        <!-- creating a request payload for client_credentials -->
        <payloadFactory media-type="xml">
            <format>
                <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                    <soapenv:Body>
                        <root xmlns="">
                            <grant_type>client_credentials</grant_type>
                        </root>
                    </soapenv:Body>
                </soapenv:Envelope>
            </format>
            <args></args>
        </payloadFactory>
    
        <!-- set related headers to call the token endpoint -->
        <header name="Authorization" expression="fn:concat('Basic ', get-property('client-authorization-header'))" scope="transport" />
        <header name="Content-Type" value="application/x-www-form-urlencoded" scope="transport" />
        <property name="messageType" value="application/x-www-form-urlencoded" scope="axis2" type="STRING" />
        <property name="REST_URL_POSTFIX" value="" scope="axis2" type="STRING" />
    
        <!-- change the token endpoint -->
        <call blocking="true">
            <endpoint>
                <http method="POST" uri-template="https://localhost:9443/oauth2/token" />
            </endpoint>
        </call>
    
        <!-- append the acquired access token and make the call to the backend service -->
        <property name="bearer-token" expression="json-eval($.access_token)" scope="default" type="STRING" />
        <property name="REST_URL_POSTFIX" expression="get-property('resource')" scope="axis2" type="STRING" />
        <header name="Authorization" expression="fn:concat('Bearer ', get-property('bearer-token'))" scope="transport" />
        <payloadFactory media-type="json">
            <format>$1</format>
            <args>
                <arg evaluator="xml" expression="get-property('request-body')" />
            </args>
        </payloadFactory>
    
        <!-- perform a send or call to complete the execution of the backend service call in EI -->
    </sequence>
    

    希望这可以帮助您开始实施。

    【讨论】:

    • 太好了。你能解释一下“REST_URL_POSTFIX”部分吗?我不是很清楚
    • REST_URL_POSTFIX 包含 API 资源的剩余 URL(例如:/api/v1/operation -> operation 将存储为 REST_URL_POSTFIX)。如果您正在调用 API /api/v1/operation,并且在序列中,如果您尝试以 https://some-host/token 调用端点,则 URL 将结果为:https://some-host/token/operation。因此,我们必须在调用实际服务时删除它并再次附加它。您可以在[这里](docs.wso2.com/display/ESB470/HTTP+Transport+Properties)了解该物业
    • 你能解释一下 调解器到底使用 for 吗?非常感谢您的帮助!
    • 在序列的中间,我们使用PayloadFactory来构造一个payload来生成Token。因此,如果您正在调用 POST 请求(这是实际的 REST 调用),那么您发送的请求负载将不会出现在 EI 中,因为我们已使用 PayloadFactory 更改了负载。因此,在最后,我们要添加另一个PayloadFactory 来再次构造实际的有效载荷以将其发送到后端。
    • 您可以使用$body(不使用json-eval($))捕获请求负载并将PayloadFactory中介的media-type更新为xml
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2021-07-15
    • 2022-01-21
    • 1970-01-01
    • 2017-11-23
    相关资源
    最近更新 更多