Skip to Content
Technical Articles

Token based authorization with OAuth 1.0 using File Lookup in sap PO

Introduction: 

We have so many blogs providing solutions to connect using OAuth 2.0. I have recently encountered an issue where the client was using OAuth 1.0 with Token-based Authorization. This document will take you through the solution where you can connect to the Rest system using Token. The token is expiring in every 4 hours.

Prerequisites:  

You should have sap PO access and target URL to access the rest system and token.

Design approach: 

We will create two Iflow. 1st Iflow will fetch token and save it into the sap PO file directory. 2nd Iflow will call the Rest service along with token retrieved from 1st Iflow. We will get token in 2nd Iflow using file lookup.

Problem Statement:

We could have used standard OAuth available in receiver Rest adapter but that allows only to connect System with OAuth 2.0. We can avoid using any java code to achieve this requirement.

Solution steps:

  1. Create 1st iflow and fetch token from Rest API. The flow would be REST to File. use the local file directory in receiver file channel.
  2. In 2nd iflow use receiver rest adapter to call REST API. Use file lookup in mapping.

Explanation:

  1. Iflow-1

The Flow would be Rest to File. Poll the token from the sender’s rest URL. My token was expiring every 4 hours so I have used a polling interval of 4 hours as below.

 

Receiver file adapter will be as below

Receiver%20file%20adapter

 

 

  1. Iflow-2:

  • You need to use a file lookup in mapping as bellow to fetch token.

 

String filename = "D:/Token/Token.txt";
String fileContentStr = "";
BufferedReader br = null;		      		 
  try{ 
   String sCurrentLine; 
   br = new BufferedReader(new FileReader(filename)); 
   while ((sCurrentLine = br.readLine()) != null){
    fileContentStr = fileContentStr + sCurrentLine;
    } 
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
    try {
     if (br != null)br.close();
    } catch (IOException ex) {
   ex.printStackTrace(); }
  }	
fileContentStr = fileContentStr.replace("\"", "");
return fileContentStr;

 

  • Use Variable substitution to get token in channel:

Use that token in HTTP Headers:

 

 

Conclusion:

This Blog gives you the solution to use token-based authentication in OAuth 1.0. You do not need to write any complex java code for this and you can achieve this using file lookup instead.

 

2 Comments
You must be Logged on to comment or reply to a post.