cancel
Showing results for 
Search instead for 
Did you mean: 

SAP PO 7.5: get token with user/password/clientID in "body"

peter_wallner2
Active Contributor
0 Kudos

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

View Entire Topic
deepika_raikode
Explorer

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,
Deepika Raikode
peter_wallner2
Active Contributor
0 Kudos

Thank you Deepika Raikode for your helpful answer. In the meantime we decided to use an ABAP proxy to trigger the call to the web service and we put a map in place to send the credentials in the body.

nkdsce99
Explorer
0 Kudos

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.

nkdsce99
Explorer
0 Kudos

Could you please provide the source blog of that code , i as i am getting multiple exceptions while using that code