Skip to Content

Passing Dynamic Oauth Token & API Key in Header of Sender REST Adapter

Hi All,

I'm configuring sender REST Pooling to consume Ariba API. I'm passing two value 1st temporary oauth token and 2nd API Key in header filed, it's working fine. API Key is constant but token is getting expire in every 27 minutes.

I have written java code to fetch token dynamically and passing it in DynamicConfiguration, but don't know where to put access_token variable in REST Adapter.

DynamicConfiguration conf = input.getDynamicConfiguration(); DynamicConfigurationKey keyHeader1 = DynamicConfigurationKey.create("http://sap.com/xi/XI/System", "HeaderFieldOne"); conf.put(keyHeader1, "Bearer "+access_token);

Please guide me with two thing

1. Java mapping code to generate token (i have written, but need sample code to understand better)

2. How to configure token variable in sender REST adapter.

ADAPTER CONFIGURATION:-

When i select REST in Message Protocol, XI Dynamic Attribute is coming but HTTP Header tab is not there, where i will mention

Authorization Bearer {access_token}?

Appreciate your time,

Binod Kumar

1.png (16.2 kB)
2.png (23.3 kB)
3.png (17.6 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Jan 11 at 04:09 PM
    Add comment
    10|10000 characters needed characters exceeded

    • Thanks Bhalchandra for your response, I'm not configuring in receiver rest adapter, I'm configuring in sender rest adapter.

      I have written java mapping to fetch token dynamically but where to pass the variable in sender side?

      Using 7.40, just help me with how to handle dynamic token in sender rest adapter

      Regards,

      Binod

  • Jan 21 at 11:27 PM

    Hello Binod,

    How do you invoke the java map if you want the token at sender side? not sure if an adapter module can be invoked (I tink not possible too as it requires message as Object).

    could you please elabborate your scenario?

    Thanks

    Sugata

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 21 at 11:35 PM

    I have created a UDF to do REST lookup to get the token and maintained that in header

    here is the code -

    String token = "";
    StringBuffer sb = new StringBuffer();
    String party = ""; // emtpy values cannot be passed from Directory to the mapping parameters.
    Channel RESTchannel = LookupService.getChannel(party,service,channel); // parameter for component and channel from mapping in ICO
    SystemAccessor accessor = LookupService.getSystemAccessor(RESTchannel);
    getTrace().addInfo("Parameters for Token Lookup - Service: "+service+", Channel: "+channel);
    try{
    InputStream inputStream;
    String reqString = "{ \"grant_type\": \"client_credentials\" }";
    getTrace().addInfo("Request: "+reqString);
    inputStream = (InputStream) new ByteArrayInputStream(reqString.getBytes());
    com.sap.aii.mapping.lookup.Payload payload = LookupService.getXmlPayload(inputStream);
    com.sap.aii.mapping.lookup.Payload result = accessor.call(payload);
    byte[] b = new byte[4096];
    for (int n; (n = result.getContent().read(b)) != -1;){
    sb.append(new String(b,0,n));
    }
    getTrace().addDebugMessage("Response: "+sb);
    String resp = sb.toString();
    // Response handling code below depends on the response structure you are receiving
    // Below is the sample response JSON
    /*
    {
    "access_token": "3927f9ac7928bf91de26c5c93d0974dd1d9c88a7",
    "expires_in": 3600,
    "token_type": "Bearer",
    "scope": null
    }
    */
    resp = resp.replace("{","");
    resp = resp.replace("}","");
    String [] respArr = resp.split(",");
    for (int i = 0;i<respArr.length;i++)
    {
    if (respArr[i].startsWith("\"access_token\""))
    {
    int p = respArr[i].indexOf(":");
    token = respArr[i].substring(p+1);
    token = token.replace("\"","");

    }
    }
    getTrace().addInfo("Token: "+token);

    }
    catch (Exception e){
    e.printStackTrace();
    }
    finally
    {

    if (accessor!=null) accessor.close();
    }
    //return token;
    token = "access_token="+token; // Parameter replace in REST URL tab in the actual receiver REST channel
    DynamicConfiguration dynConf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
    DynamicConfigurationKey key = DynamicConfigurationKey.create( "http://sap.com/xi/XI/System/REST", "access_token"); dynConf.put(key, token);
    return ""

    Add comment
    10|10000 characters needed characters exceeded

    • Yeah, I was not able to find where we need to put access_token variable in sender Rest adapter.

      1. For sender Rest adapter UDF is fine and in receiver we can go for java mapping & UDF is that correct ?

      2. Can you please provide brief details on how to use UDF for dynamic token with complete code? and where you're passing following details

      client_id

      client_secret

      3. Recently i came across receiver rest adapter project, i have developed standalone complete java application for that, i was not able to complete by rest adapter, PI7.40

      Let me explain you the scenario :

      1. In 1st call we will get token

      2. in 2nd call with the help of token, it generates ChunkURI

      3. In 3rd call, have to make post call on ChunkURI along with passing the file

      I tested through postman, it was working fine but through PI OMG so much horrible

      -> PI 7.40 is not able to handle token automatic

      -> any how i passed the token in header

      -> created the ChunkURI as well

      -> but last step i sucked, i created the java code for file attachment but there were no option to put dynamic variable

      If you could provide any solution for it, it would be great.

      ----------------------------

      Related to REST adapter basic question still i have

      1. what is better solution to handle dynamic token in sender & receiver side as well ??



      Lot's of thanks for your support,

      Binod

  • Feb 05 at 08:13 AM

    Hi Binod,

    You can create a OData service in your SAP gateway to call Ariba to get the data in SAP and from there you can also trigger a scenario - proxy to jdbc.

    Thanks

    Sugata

    Add comment
    10|10000 characters needed characters exceeded