从 cognito 获取令牌并在 wso2 Enterprise Intigrator 中缓存

getting token from cognito and caching in wso2 Enterprise Intigrator

我需要从 Cognito 获得令牌,但我不知道如何通过企业集成商获得它。我还有 client_idclient_secet 以及端点。但是我无法获得令牌,似乎我没有使用正确的参数正确调用端点。

After getting the token I need to cache it till the token expires so I don't have to call again and again

这是我正在尝试的代码

<cache id="cache-sample" scope="per-host" collector="false" hashGenerator="org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator" timeout="5000">
    <implementation type="memory" maxSize="1000"/>
</cache>
<header name="client_id" scope="transport" value="XXXXXXXXXXXXXXXXX"/>
<header name="client_secret" scope="transport" value="XXXXXXXXXXXXXXXXXXX"/>
<header name="Content-Type" scope="transport" value="application/json"/>
<property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
<call>
    <endpoint>
       <address uri="https://xcxxxxxxxxxx.auth.us-east-1.amazoncognito.com/oauth2/token"/>
    </endpoint>
</call>
<property name="RESPONSE" value="true" scope="default" type="STRING"/>
<cache scope="per-host" collector="true"/>
<respond/>
<?xml version="1.0" encoding="UTF-8"?>
<api context="/api/myService" name="myService-api" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST" uri-template="/getToken">
        <inSequence>
            <script language="js"><![CDATA[var payload = mc.getPayloadXML();
                var log = mc.getServiceLog();
                var client_id = payload..*::client_id.toString();
                var client_secret = payload..*::client_secret.toString();
                mc.setProperty("client_id", client_id);
                mc.setProperty("client_secret", client_secret);]]>
            </script>
            <payloadFactory media-type="json">
                <format/>
                <args/>
            </payloadFactory>
            <property name="ContentType" scope="axis2" type="STRING" value="application/x-www-form-urlencoded"/>
            <property expression="fn:concat($ctx:client_id,':',$ctx:client_secret)" name="credentials" scope="default" type="STRING"/>
            <property expression="fn:concat('Basic ', base64Encode($ctx:credentials))" name="Authorization" scope="transport" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
            <property name="FORCE_POST_PUT_NOBODY" scope="axis2" type="BOOLEAN" value="true"/>
            <property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
            <cache 
                collector="false" 
                hashGenerator="org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator" 
                id="cognito-token" 
                scope="per-host" 
                timeout="3600">
                <onCacheHit>
                    <log level="custom">
                        <property expression="/" name="[usage-service-api]:: CHACHE HIT"/>
                    </log>
                    <respond/>
                </onCacheHit>
                <implementation maxSize="50" type="memory"/>
            </cache>
            <call>
                <endpoint>
                    <http method="post" uri-template="https://xxxxxxxxxxx.amazoncognito.com/oauth2/token?grant_type=client_credentials"/>
                </endpoint>
            </call>
            <property name="RESPONSE" scope="default" type="STRING" value="true"/>
            <cache collector="true" id="cognito-token" scope="per-host"/>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

For first Call cache miss will happen in that case it will call the endpoint and store it in the cache for the second call and onwards till the time expires for the same call it will hit the cache i.e. Cache hit then It will respond from onCacheHit mediator

<onCacheHit>
    <respond/>
</onCacheHit>

But after cache hit it will skip any execution of any statement written after cache Mediator. So So keep it in some other sequence if you are writing in some sequence You can have more than one resources in API.

如果你想知道如何调用这个API然后看这个

<log level="custom">
    <property expression="/" name="[usage-service-api]:: CHACHE HIT"/>
</log>

上面的代码块记录了 CACHE HIT

上缓存的内容