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”:
After selecting this type, we can input key and values as following or we can use “Bulk Edit” to maintain them in bulk mode:
2.2. Maintaining Endpoint and Authorization
Rather than maintaining Body, we should maintain also Method, Endpoint and Authorization (in our example is Basic):
2.3. Maintaining Header
Furthermore, we should add “Content-Type: application/x-www-form-urlencoded” as a header parameter:
2.4. Generating Token
Once we send the request, access token will be generated:
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:
In the next screen, we select “REST” as web service type and press “Next”:
Then we give Endpoint for generating access token and press “Next”:
Afterwards, we select “Create Communication Scenario” and determine its name via “Communication Scenario Name” and and press “Next”:
Finally, we review it and and press “Finish”:
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”:
Afterwards, we will be redirected to a C4C screen in which we can create a new communication arrangement:
In the next step, we select the created communication scenario in SAP Cloud Application Studio:
Then, we select the created web service in SAP Cloud Application Studio:
In the next screen, we can maintain “Authentication Method” and “Credentials”:
Now, we are done and can finish and activate the communication arrangement:
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 “&”.
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:
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.