on 08-04-2023 10:36 PM
Dear experts,
we have the requirement to get a token from an external API. Using a user, password and client ID. However, these 3 parameters have to be in the body of the request. So in Postman this is successful:
we are getting an access token and a refresh token back
How do we configure this in a REST Sender Polling adapter, we have this so far which gives us a "415 Unsupported Media Type":
Do we need a map that creates an XML like this? But how would the REST Sender Adapter convert that to a JSON?
{
"userName": "adfadf",
"password": "adfadf",
"clientId": "adfadf"
}
Thank you for any tips on this.
Kind regards, Peter
Hi Peter,
In a similar scenario where I had to send the credentials in the body of the message, I wrote a UDF because the OAuth with Grant type was not working. This option would add the credentials in the Header but if you see the postman, header does not contain the credentials instead it is in the body. To align the REST channel with the postman, please try to use the below piece of code.
Channel channel= LookupService.getChannel(commComp,commChann);
SystemAccessor accessor = LookupService. getSystemAccessor(channel);
StringBuffer sb = new StringBuffer();
String token = "";
String accessToken="";
try{
InputStream inputStream;
// String reqString ="."; //This is to avoid XMLPayload is empty Exception
//String reqString = "clientId=clientId&userName=userName&password=password";
clientId= "clientId="+clientId;
userName="&userName="+userName;
password="&password="+password;
String reqString = clientId+userName+password;
this.getTrace().addInfo("text is" +reqString);
inputStream = (InputStream) new ByteArrayInputStream(reqString.getBytes());
Payload payload = LookupService.getXmlPayload(inputStream);
Payload RESTOutPayload = accessor.call(payload);
InputStream inpStr = RESTOutPayload.getContent();
int bufferSize = 1024;
char[] buffer = new char[bufferSize];
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(inpStr, StandardCharsets.UTF_8);
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
out.append(buffer, 0, numRead);
}
String result = out.toString();
accessToken = result.substring(result.indexOf("{\"access_token\":") + "{\"access_token\":".length()+1, result.indexOf(",\"token_type\":")-1);
this.getTrace().addInfo(" Access Token - " + accessToken);
this.getTrace().addInfo(" responseStr value is " + result);
}
catch (Exception e){
token=e.getMessage();
this.getTrace().addInfo("token value from exception block is " +token);
e.printStackTrace();
}
finally {
if (accessor!=null) accessor.close();
}
return accessToken;
Hope this helps.Regards,You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Deepika , I have a similar requirement to pass , username & password in a json body for our token APi to fetch the token. In your above UDF how exactly are you using it , if u can provide some details like what is the inputs to the above udf and also if token caching can be enabled with this.
User | Count |
---|---|
76 | |
9 | |
7 | |
6 | |
6 | |
6 | |
6 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.