首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >API EI:我是否可以使用mediator来请求另一个WSO2,并将其响应传递给body请求?

API EI:我是否可以使用mediator来请求另一个WSO2,并将其响应传递给body请求?
EN

Stack Overflow用户
提问于 2021-07-05 15:12:37
回答 1查看 178关注 0票数 0

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-05 16:00:01

您可以使用中介序列来实现这些需求。您可以参考此blog了解有关如何根据您的需求开发序列的更详细说明。该博客是为API Manager产品编写的,但是,您也可以在EI中使用相同的方法来完成它。

示例中介序列如下所示

代码语言:javascript
复制
<?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>

希望这能帮助你从实现开始。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68252011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档