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:
- 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.
- In 2nd iflow use receiver rest adapter to call REST API. Use file lookup in mapping.
Explanation:
-
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
-
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.
Thats a good one ,keep blogging..
Thank you Bhaskar!
Hi
Thanks for the blog. Please let me know if I understood this blog correct.
–> Location should be NFS location
3. Please share fetched token.txt file
Regards,
Rashmi
Hi!
Iflow-1:
How do you deal with the results of Rest Polling HTTP POST ?
In my case when the polling executes, I receive only a string (the TOKEN), it's not a JSON structure and it's not a XML structure, so I get an error in PI REST SENDER channel:
"Invalid JSON message content used"
-> in the case I have checked Data Format/Data Type JSON.
and
"Catching exception calling messaging system: Unable to parse XML message payload to extract operation for receiver determinationorg.xml.sax.SAXParseException;"
-> in the case I have checked Data Format XML.
Best regards,
Dylon.
Hi
Thanks for the Blog.
We have a Problem with the Rest Sender and get the value "IsVerified:False". Did somebody get the same issue? The ping is fine. Can somebody help us here?
Token_Problem
Hi,
Thanks a lot.
I got the token, but i hava a question.
I use three parameters(Fixed value) to get token as bleow, and we have another system "SRM" thatuses same interface but not by po.
{
"appId": "A",
"appSecret": "B",
"permanentCode": "C"
}
Now Token is valid for 120 minutes,so i set polling interval of 120 minutes,first,'SRM' get token,at the 100th minute,i get token by po,at the 120th minute,my token has expired,my token is only valid for 20 minutes.
Best regards.
Hi All,
Just have a question, in Iflow 2 is it possible to use that file lookup in the sender channel?
Iflow 1 - Rest (polling) -> File (for the token)
Iflow 2 - Rest (polling) -> File
Appreciate your inputs.
Thanks
Best Regards