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":
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 02: Key and Values - Body
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 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 05: Request Header
2.4. Generating Token
Once we send the request, access token will be generated:
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 07: Adding External Web Service Integration
In the next screen, we select "REST" as web service type and press "Next":
Image 08: Determine Web Service Type
Then we give Endpoint for generating access token and press "Next":
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 10: Create Communication Service
Finally, we review it and and press "Finish":
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":
Image 12: Manage Communication Arrangement
Afterwards, we will be redirected to a C4C screen in which we can create a new communication arrangement:
Image 13: Create a new Communication Arrangement 01
In the next step, we select the created communication scenario in SAP Cloud Application Studio:
Image 14: Create a new Communication Arrangement 02
Then, we select the created web service in SAP Cloud Application Studio:
Image 15: Create a new Communication Arrangement 03
In the next screen, we can maintain "Authentication Method" and "Credentials":
Image 16: Create a new Communication Arrangement 04
Now, we are done and can finish and activate the communication arrangement:
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 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:
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 (
mhsalmani) 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.