Skip to Content
Technical Articles

Cloud Platform Integration OAuth2 Credentials

Hi Integrators,

Welcome to my Cloud Platform Integration OAuth2 Credentials blog!

Nice to have you here, it means you take security seriously.

Like we all should!

 

Introduction 📖

The reason I wrote this blog is because back then, I didn’t find a lot of information or examples on how to use OAuth2 in CPI Flows.

What I really wanted to avoid was performing a request to fetch the access token and pass it throughout the CPI Flow to perform other requests.

This because it just doesn’t feel right and safe and it is not best practice at all.

Then how to handle these tokens in a proper way?

I figured out how to do it the right way thanks to: Deepak Govardhanrao Deshpande

With his very detailed and nice blog about: SAP Cloud Platform Integration – OAuth2 Client Credentials Support in OData V2 Adapter

It gave me the insight on how to use the OAuth2 credentials in CPI Flows. So thanks a lot Deepak Govardhanrao Deshpande.

Then what will I talk about?

I will show how to use this OAuth2 Credentials with 2 examples that are currently a hot topic in my option. This by showing how to use the SAP Leonardo IoT APIs and the SuccessFactors Learning (LMS) APIs.

This because they both use bearer tokens as authentication/authorization. This can be handled perfectly by the CPI OAuth2 credentials.

 

How NOT to handle your access tokens 👎

Like I mentioned we do not want to “manually” fetch our tokens in our flow. If we would do this, we should start extracting it and set it as Authorization header and so on…

 

This would also bring the following disadvantages:

– The flow will get bigger

– The flow will be less clear to read

– The authorization header needs to be removed in some cases

– The token can expire

– The token can be visible in logs

– …

 

All reasons why we would not go for a solution like that.

 

 

 

How to handle your tokens 👍

Then how to handle those tokens?

We store the credentials in the OAuth2 credentials in the CPI Security Material.

Once you did that you can just perform the request with the authentication type OAuth2 Client Credentials, and the tokens are taken care of automatically.

 

In the overview dashboard of your SAP Cloud Platform Integration Tenant, you go to Manage Security > Security Material.

 

Here you choose Add > OAuth2 Credentials.

 

 

 

OAuth2 & SAP SuccessFactors Learning API

How to configure the OAuth2 Credentials for the Learning API for the LMS Admin. (user also exists)

Name lms_admin_OAuth2
Grant Type Client Credentials
Description Perform LMS calls as admin.
Token Service URL https://{{YOUR-COMPANY-ID}}-stage.plateau.com/learning/oauth-api/rest/v1/token
Client ID Your client id, is the same as your company id
Client Secret Your client secret
Client Authentication Send as Request Header
Include Scope Check Checkbox
Scope { “userId”: “{{YOUR USER ID}}“, “companyId”: “{{YOUR COMPANY ID}}“, “userType”: “admin“, “resourceType”: “learning_public_api” }
Content Type application/json

 

Now you can just perform your HTTP Request without extra steps to handle the tokens.

As you can see the HTTP Request is performed on the following ITEMS URL.

(Change your company id)

The Authentication is OAuth2 Client Credentials and you provide the name you set in the Security Material.

You deploy the flow and you’ll see the flow executed successfully.

Next your SFSF Learning data is available in the logged attachment.

 

 

OAuth2 & SAP Leonardo IoT API

When you want to make use of the SAP Leonardo IoT APIS in CPI Flows, it will just work the same.

You enter your OAuth2 Credentials as follows:

Name Leonardo-IoT
Grant Type Client Credentials
Description IoT Application Enablement
Token Service URL https://{{YOUR-IoT-TENANT}}.authentication.eu10.hana.ondemand.com/oauth/token
Client ID Your client id
Client Secret Your client secret
Client Authentication Send as Request Header
Include Scope Uncheck Checkbox
Scope
Content Type

 

Again now you can just perform your HTTP Request without extra steps to handle the tokens.

Now you only need to pass an address and optional query.

Example

Address https://details-thing-sap.cfapps.eu10.hana.ondemand.com/CompositeThings/ThingType/v1/{{TENANT-PACKAGE}}:{{THING-TYPE}}/Things(‘{{THING-ID}}‘)
Query $expand=DYN_ENT_{{TENANT-PACKAGE}}__{{PROPERTY-SET}}
Authentication OAuth2 Client Credentials
Credential Name Name you provided when you set it in the Security Material. (Leonardo-IoT)

 

Execute the flow and see your flow executed successfully.

Now your IoT data is logged in the measurements and accessible in your CPI-Flow.

 

What did we learn? 📙

In this blog I went over the advantages of using the OAuth2 Credentials in CPI flows.

I demonstrated the usage of OAuth2 credentials in CPI flows by making use of the SFSF Learning APIs and SAP Leonardo IoT APIs.

From now on you are able to use the OAuth2 credentials in every CPI flow you will build in the future.

 

Message of this blog?

Never pass the tokens “manually” anymore!

Thanks for reading and happy Integrating!

 

Kind regards,

Dries

 

 

15 Comments
You must be Logged on to comment or reply to a post.
    • Hi Daniel,

      Thanks for the feedback and question.

      The way I implemented it in the examples, it will fetch a new token every time it performs the call to fetch the data.

      If I’m not mistaken, the IoT Bearer token expires every 15 minutes  by default.

      I would suggest not to cache the tokens in such a specific case, since this would bring extra validity checks with it.

      If you really would like to cache them, I think you should call the tokens manually (which I not recommend).

      But good question!

      Thanks a lot!

      Kind regards.

      Dries

       

       

       

  • Hi Dries,

    Great blog. I wanted to check if it is possible to use the OAuth2 credentials in SOAP adapter. I have a requirement where I have to send the following details in SOAP header. I tried using the OAuth2 method in both cases – SOAP and HTTP but somehow it is not working. Maybe because of the additional node – RequestHeader.

    <RequestHeader>

    <session_token></session_token>

    <client_id></client_id>

    </RequestHeader>

    • Hi Mala,

      Thank you for your feedback!

      I do not have so much experience with SOAP myself to be honest.

      With HTTP in both of the above cases it should work fine.

      I just had a look at the authentication options with SOAP in CPI and I do not see the option:

      So as far as I can tell I do not think it is possible to use the OAuth2 Client Credentials option as Authentication.

      Therefor it is maybe not supported in the SOAP Adapter either.

      Kind regards,

      Dries

       

       

       

      /
  • Hi Dries,

     

    How can we use EC odata Api to CPI?

    I tried but I am getting the below error:-

    Error Details
    com.sap.it.rt.adapter.sfsf.core.exception.SfsfException: Invalid type of credential provided; alias ‘SF_CRED’ is of credentail type ‘successfactors’ instead of ‘oauth2:default’
    Thanks,
    Anand
    • Hi Bhaskar,

      Thank you for your question.

      Are you trying to connect to the Successfactors OData API or to the Learning (LMS) APIs?

      In case you are trying the LMS APIs, this blog should help you.

      When you want to connect to the Successfactors APIs you could use Basic Authentication to access the SFSF OData API or OAuth.

      To me it looks like you are trying to connect to LMS with the OAuth credentials of SFSF.

      You can find out the correct API endpoint (LMS) if you follow these steps and configuration in the LMS module:

      https://apps.support.sap.com/sap/support/knowledge/public/en/2318897

      Be careful to not regenerate new client secrets if they are used somewhere else. Check with your SFSF team.

      Via the documentation above you should be able to find the correct API endpoint and you can test it via a HTTP-rest  client like Postman.

      Once you found out the correct endpoint and credentials, you can follow this blog again to retrieve your data.

      Hope this helps.

      Kind regards,

      Dries

       

       

  • Hello Dries,

    Thanks a lot for your blog! For now, I have a similar situation with manual access token setting.

    Is it possible to do that in SAP Cloud Platform Cockpit via Destination setup (cloud foundry)?

    Thanks.

    Kind regards,

    Kiryl

    • Hello Kiryl,

      Thanks for the feedback and your question!

      I’m not sure if that’s possible, if you would try to achieve this (Cloud Foundry) you would have to connect to your destination instance with your service keys and read out all your destinations and filter them on the one you need. Still, that way you would have to perform the calls to retrieve a token (on you token URL endpoint) and pass it along the CPI-Flow.

      If you want to achieve something like this I would suggest (just like the blog says) to place your OAuth URLs id’s and secrets in the CPI Security > OAuth2 Credentials. Next you place the url you want to perform a call on inside your flow and you choose OAuth2 Client Credentials. CPI will take care of your authentication while your perform the request.

      I think you should implement it like this instead of using the destinations in this case.

      Hope this answers your question.

      Kind regards,

      Dries

  • Hi Dries ,

     

    I have followed the same process as mentioned in your blog but when I perform HTTP request  , getting response as 401 unauthorized.

    Could you let me know where I might have done mistake.

    Regards,

    Abhi

    • Hi Abhilash,

      Thanks for your feedback and awesome you are trying it out!

      Just for my understanding, are you trying out the LMS or the IoT scenario?

      Also the first step during the debugging process of such an error, is having a look whether you can access the API’s via an http-client such as Postman.

      Via Postman you can try to retrieve your access token and once retrieved, try to get the data.

      If you cannot retrieve the data with your token, it could be something regarding user scopes or roles.

      But if you would not be able to fetch your access token, it would be probably an error with wrong credentials and the authentication payload.

      Did you try it already via Postman?

      If you can exclude this in Postman you made some progress already, otherwise it has probably something to do with the credentials and you would have to check the systems for your user credentials and access rights.

      Thanks a lot for your comment and good luck already!

      Kind regards,

      Dries

  • Hi,

    Have you ever tried connecting to Successfactors OData via OAuth2 but using “OAuth2 SAML Bearer Assertion (SuccessFactors)”?

    Any suggestions?

    Kind regards.

    /
  • Hi,

    Is it possible to pass optional parameters?

    It looks something like this with JAVA code:

    Map<String, String> optionalParameters = new HashMap<>();optionalParameters.put(“response_type”, “token”);
    optionalParameters.put(“authorities”, “{\”az_attr\”:{\”ZoneID\”:\”AN02002005169\”, \”external_id\”:\”abcd1234\”}}”);OAuth2TokenService oAuth2TokenService = new XsuaaOAuth2TokenService(new RestTemplate());
    String access_token = oAuth2TokenService.retrieveAccessTokenViaClientCredentialsGrant(
    URI.create(tokenURL), new ClientCredentials(clientId, clientSecret), null, optionalParameters,true).getAccessToken();

     

    Kindly advice if in security material something like this can be maintained for the optional parameters with OAuth apart from client id and client secret.

     

    Thanks and Regards,
    Vineet.