WSO2 EI:我可以使用中介来请求另一个 API 并将其响应传递给正文请求吗?

问题描述

就我而言,我想向标头 mediator 添加一个动态值(“Bearer”+ {access-token})。 所以在头中介之前,我想调用一个 get-token API 并从它的响应中提取 {access-token} 元素。我怎样才能得到那个?非常感谢。

解决方法

您可以使用中介序列来满足此类要求。您可以参考此 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>

希望这有助于您开始实施。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...