Skip to Content
0
Jun 05, 2022 at 06:09 PM

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

175 Views

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"