Skip to Content
Technical Articles
Author's profile photo Mohammad Salmani

Generating Access Token Via REST API with Body of type Form (x-www-form-urlencoded) using ABSL Code in SAP Cloud Application Studio

1. Introduction

In this blog, we will learn, how to generate Access Token Via REST API with Body of type Form using ABSL Code in SAP Cloud Application Studio. We will at first show how we can do the same using Postman and afterwards go to the details of sending this request using ABSL Code.

2. Sending Request Using Postman

2.1. Maintaining Body

In Postman we can send such Body using “x-www-form-urlencoded”:

Body%20in%20type%20Form

Image 01: Body in type Form

After selecting this type, we can input key and values as following or we can use “Bulk Edit” to maintain them in bulk mode:

Image%2002%3A%20Key%20and%20Values%20-%20Body

Image 02: Key and Values – Body

Image%2003%3A%20Key%20and%20Values%20in%20Bulk%20Edit%20-%20Body

Image 03: Key and Values in Bulk Edit – Body

2.2. Maintaining Endpoint and Authorization

Rather than maintaining Body, we should maintain also Method, Endpoint and Authorization (in our example is Basic):

Image%2004%3A%20Endpoint%20and%20Authorization

Image 04: Request Endpoint and Authorization

2.3. Maintaining Header

Furthermore, we should add “Content-Type: application/x-www-form-urlencoded” as a header parameter:

Image%2005%3A%20Request%20Header

Image 05: Request Header

2.4. Generating Token

Once we send the request, access token will be generated:

Image%2005%3A%20Sending%20Request

Image 06: Sending Request

3. Sending Request Using ABSL Code in SAP Cloud Application Studio

3.1. Add a new External Web Service

Now, we can implement the same request using SAP Cloud Application Studio. In this regard, we should at first add a new “External Web Service Integration” to the desired solution:

Image%2007%3A%20Adding%20External%20Web%20Service%20Integration

Image 07: Adding External Web Service Integration

In the next screen, we select “REST” as web service type and press “Next”:

Image%2008%3A%20Determine%20Web%20Service%20Type

Image 08: Determine Web Service Type

Then we give Endpoint for generating access token and press “Next”:

Image%2009%3A%20Configure%20REST%20API%20Endpoint

Image 09: Configure REST API Endpoint

Afterwards, we select “Create Communication Scenario” and determine its name via “Communication Scenario Name” and and press “Next”:

Image%2010%3A%20Create%20Communication%20Service

Image 10: Create Communication Service

Finally, we review it and and press “Finish”:

Image%2011%3A%20Review%20and%20finalizing%20the%20Web%20Service

Image 11: Review and finalizing the Web Service

3.2. Managing Communication Arrangement

Now, we can maintain communication arrangement by right click on the Communication Scenario in SDK and select “Manage Communication Arrangement”:

Image12%3A%20Manage%20Communication%20Arrangement

Image 12: Manage Communication Arrangement

Afterwards, we will be redirected to a C4C screen in which we can create a new communication arrangement:

Image13%3A%20Create%20a%20new%20Communication%20Arrangement

Image 13: Create a new Communication Arrangement 01

In the next step, we select the created communication scenario in SAP Cloud Application Studio:

Image15%3A%20Create%20a%20new%20Communication%20Arrangement%2002

Image 14: Create a new Communication Arrangement 02

Then, we select the created web service in SAP Cloud Application Studio:

Image%2015%3A%20Create%20a%20new%20Communication%20Arrangement%2003

Image 15: Create a new Communication Arrangement 03

In the next screen, we can maintain “Authentication Method” and “Credentials”:

Image16%3A%20Create%20a%20new%20Communication%20Arrangement%2004

Image 16: Create a new Communication Arrangement 04

Now, we are done and can finish and activate the communication arrangement:

Image1%3A%20Create%20a%20new%20Communication%20Arrangement%2004

Image 17: Create a new Communication Arrangement 04

3.3. Developing an ABSL Function to Send the Request

Then, we can develop an ASBL function to send the request and generate Access Token. We should determine ServiceName and ServiceScenario based on what we created already in SAP Cloud Application Studio and then define HttpMethod. Because we want to send a body of type “Form”, ContentType should be “application/x-www-form-urlencoded” and Key and Values of Body should be separated by ampersand “&”.

Image%2018%3A%20Send%20a%20Request%20using%20ABSL%20Code

Image 18: Send a Request using ABSL Code

As we see above, we should use “WebServiceUtilities.ExecuteRESTServiceWithoutEncoding” to send such a requests with Body of type “Form” and after getting the response we can pars it and find access token.

We can use one of the following methods to pars a sting in JSON format:

  • Develop a custom function:

How to Parse a String in JSON Format in ABSL

  • Use the standard functionality of SAP Cloud Application Studio:

Parse JSON format in ABSL using Cloud Application Studio

3.4. Debugging the Function and Generating Token

If we debug the function, we can see the generated Token as well:

Image19%3A%20Generate%20Access%20Token%20using%20ABSL%20Code

Image 19: Generate Access Token using ABSL Code

 

4. Conclusion

As a conclusion, we are now able to develop an ABSL function for generating an Access Token Via REST API with Body of type Form. This token can be even used in other functions or sent to UI for loading as an example a Mashup.

5. Call for Action

I would like to request you to share feedback or thoughts in a comment and follow my profile (Mohammad Salmani) for future posts.

If you have any question regarding SAP Cloud Applications Studio you can ask it here or even read other blogs through this link.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.