问题
I'm trying to get access token through AWS Cognito with client credentials but getting something else.
I'm doing this in wso2 Enterprise integrator 6.1.0
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema/instance">
<soapenv:Header>
<Content-Type xmlns="">$1</Content-Type>
<Authorization xmlns="">$2</Authorization>
</soapenv:Header>
<soapenv:Body/>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="xml" value="application/x-www-form-urlencoded"/>
<arg value="Basic 2354sdfmdtrerkdfdgkeryryrtwdasr345345twsdfwsedtr34"/>
</args>
</payloadFactory>
<log level="full"/>
<property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
<endpoint key="validateUser-ext-ep"/>
</call>
<log level="full"/>
after this the response i'm getting is like this :
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns:binary xmlns:ns="http://ws.apache.org/commons/ns/payload">PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbiYWNrZ3JvdW5kLWN1c3RvbWl6YWJsZSBtb2RhbC1jb250ZW50LW1vYmlsZSI+CiAgICAgICAgICAgICAgICA8ZGl2PgogICAgICAgICAgICAgICAgICAgIDxkaXYgY2xhc3M9ImJhbm5lci1jdXN0b21pemFibGUiPgogICAgICAgICAgICAgICAgICAgICAgICA8Y2VudGVyPgogICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgIDwvY2VudGVyPgogICAgICAgICAgICAgICAgICAgIDwvZGl2PgogICAgICAgICAgICAgICAgPC9kaXY+CiAgICAgICAgICAgICAgICA8ZGl2IGNsYXNzPSJtb2RhbC1ib2R5Ij4KICAgICAgICAgICAgICAgICAgICA8cCBhbGlnbj0iY2VudGVyIj5BbiBlcnJvciB3YXMgZW5jb3VudGVyZWQgd2l0aCB0aGUgcmVxdWVzdGVkIHBhZ2UuPC9wPgogICAgICAgICAgICAgICAgICAgIDxicj4KICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIDwvZGl2PgogICAgICAgICAgICA8L2Rpdj4KICAgICAgICA8L2Rpdj4KICAgIDwvZGl2Pgo8L2JvZHk+Cgo8L2h0bWw+Cg==</ns:binary>l
</soapenv:Body>
</soapenv:Envelope>
I don't know if I'm doing something wrong as in postman I sent the data same way I was getting the token there I passed the Authrization
as Basic dfudne4r49859dfnw34598sdfs
base64 endcoded(client:client_secret) and Content-Type : application/x-www-form-urlencoded
in header and in params I passed grant_type: client_credential
for this I was able to get the token but when I tried in wso2 esb I got the above error
the endpoint looks like :https://xxxxxxxxxx.us-east-1.amazoncognito.com/oauth2/token?grant_type=client_credentials
回答1:
this is the API to get the Access token and it returns the access token
<?xml version="1.0" encoding="UTF-8"?>
<api context="/api/myService" name="my-service-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"/>
<call>
<endpoint>
<http method="post" uri-template="https://xxxxxxxxxxx.amazoncognito.com/oauth2/token?grant_type=client_credentials"/>
</endpoint>
</call>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Call the above API like this :
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<root>
<client_id>$1</client_id>
<client_secret>$2</client_secret>
</root>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="xml" expression="get-property('client_id')"/>
<arg evaluator="xml" expression="get-property('client_secret')"/>
</args>
</payloadFactory>
<property name="ContentType" scope="default" type="STRING" value="application/xml"/>
<property action="remove" name="REST_URL_POSTFIX" scope="axis2"/>
<call>
<endpoint>
<http method="post" uri-template="http://localhost:8280/api/myService/getToken"/>
</endpoint>
</call>
<script language="js">
<![CDATA[ var tokenContainer = mc.getPayloadJSON();
var log = mc.getServiceLog();
mc.setProperty("Authorization_header", tokenContainer.access_token);]]>
</script>
The Response will be
{"access_token":"LzSq94JoaUT2LwJlkEl35CXX0MdwqtUKIL8Wvi7dm4SqcSofR4xF5xBZre83MZXpHOr-Hg","expires_in":360,"token_type":"Bearer"}
In last script mediator you can access the token Access token throught getting it by mc.getPayloadJSON()
this will give the same response as shwon above
来源:https://stackoverflow.com/questions/60560441/getting-cognito-acces-token-with-wso2-esb