cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to get Azure AD token is returning token type but not returning the access_token

former_member186472
Participant
0 Kudos

Hello PO Gurus, We have built an UDF to get the Azure token. It is returning <token_type>. but not returning <access_token>.

When we tested from postman we received below response.

"token_type": "Bearer",
"expires_in": 3599,
"ext_expires_in": 3599,
"access_token": "actual token string"

But when we test UDF we could see only below result, with <token_type>.

<token_type>Bearer</token_type>

Below is the code in UDF.

String token ="";
 try {

String tokenType="bearer ";

Channel channel = LookupService.getChannel("BC_XXXXC_ADFS_TOKEN","CC_ADFS_APIGEE_REST_RECEIVER");
SystemAccessor accessor = LookupService.getSystemAccessor(channel);                                           
String RequestString = "grant_type=client_credentials"+  "≻ope="+ scope +  "&client_id=" +appId +  "&client_secret="+appSecret;
InputStream inputStream =new ByteArrayInputStream(RequestString.getBytes());                                          
XmlPayload RequestPayload = LookupService.getXmlPayload(inputStream);																											 
Payload ResponsePayload = null;
ResponsePayload = accessor.call(RequestPayload);
InputStream in = ResponsePayload.getContent();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader reader =    new BufferedReader(isr);
String responseString = "";
			String outputString = "";
			while ((responseString = reader.readLine()) != null) {
				outputString = outputString + responseString;
			}
if (outputString.indexOf("access_token\":\"") > -1) {
				int i1 = outputString.indexOf("access_token\":\"");
				String str1 = outputString.substring(i1 + 15);
				int i2 = str1.indexOf("\"}");
				String str2 = str1.substring(0, i2);
				token = str2;
 token = tokenType+ token	
} getTrace().addInfo("token->>"+token); DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION); DynamicConfigurationKey Auth = DynamicConfigurationKey.create("http://sap.com/xi/XI/System", "HeaderFieldTwo"); conf.put(Auth,token); } catch (Exception e) { getTrace().addInfo("ADFS Token Error--" + e.getMessage()); } return "token";

We have not used out of the box solution from SAP PO to get token using oAuth General tab settings from REST Receiver channel, due to token URL is external and API URL is internal. We get token but API URL connection fails with "Host not reachable"

Accepted Solutions (0)

Answers (2)

Answers (2)

kottiyil_ramnath
Participant
0 Kudos

Hi Siva,

Your InputStream which is,
InputStream in = ResponsePayload.getContent();

Regards,

Ramnath

kottiyil_ramnath
Participant
0 Kudos

Hi Siva,

Try,

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document document = builder.parse(in);
  NodeList list = document.getElementsByTagName("token");
  Node node = list.item(0);
  if (node != null) {
  node = node.getFirstChild();
  if (node != null) {
  Token = node.getNodeValue();
  }
  }

If this does not return the token value, check whether your input parameters are correct. Use XPI_Inspector with the soap channel to check whether the input to token service is going as expected. Thanks,

Regards,

Ramnath

former_member186472
Participant
0 Kudos

Thanks for your response Ramnath.

One question, what is "in", is this response payload?? or streaminput.

Appreciate your quick response.

Best Regards,

Siva