Configuring OAuth 2.0 and Creating An ABAP Program That Uses OAuth 2.0 Client API - SAP Blogs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.

0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Community

 Do you have an S- or P- account? 


If so, you need SAP Universal ID. In a few months, SAP Universal ID will be the
Get started with SAP Universal ID
only option to login to SAP Community. Without it, you will lose your content and
badges. If you have multiple accounts, use the Consolidation Tool to merge your
content.

Ask a Question Write a Blog Post Login

Technical Articles

Lakshmi Ganga
December 18, 2020 | 4 minute read

Configuring OAuth 2.0 and Creating an ABAP Program


That Uses OAuth 2.0 Client API
 34  21  45,403
Follow

This blog post will give the basic overview about OAuth2.0 Configuration and use case from SAP ABAP program.
 Like

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 1/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

 RSS Feed Introduction:


The OAuth 2.0 server (AS ABAP) protects resources you want to use, and the OAuth 2.0 client enables you to access
services and resources that are offered by a service provider.

Authentication with OAuth 2.0 protection between an SAP NetWeaver Application Server for ABAP and an external service
provider such as, for example, SAP HANA Cloud Platform, Google Cloud Platform, or Microsoft Azure, requires a dedicated
OAuth 2.0 client. You can configure and register this OAuth 2.0 client in the OAuth 2.0 server (AS ABAP).

The OAuth 2.0 client enables end users to easily access a service provider with the same credentials they are already
using in the service provider. The communication between OAuth 2.0 client and server is secured by an HTTPS
connection. The end users can then use services and resources offered by a service provider, for example, SAP HANA
Cloud Platform or Microsoft Azure, to edit or process their data that is located as resources on the AS ABAP. During the
authentication, the OAuth 2.0 client passes the OAuth 2.0 scopes to the service provider. The OAuth 2.0 scopes contain
references to the allowed resources.

So first, lets try to understand from POSTMAN. How to call the OAuth2.0 enabled endpoint.

POSTMAN:
Use the GET call with the main API endpoint. In the authentication, select the type as ‘OAuth2.0’.

Based on the service provider, select the grant type on the right hand side. I have selected as Client Credentials. Provide
the Access Token URL, Client ID and Client Secrete. Also provide the scope as configured at the service provider. Select
Client Authentication as ‘Send as Basic Auth header’ and click on Get New Access Token.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 2/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Now perform the GET call and set any header parameters if required.

We get the status as 200 and response from the service provider.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 3/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Now we will call the OAuth2.0 enabled endpoint from ABAP program using OAuth2.0 configuration.

Refer to the SAP help which has quite good amount of information on the process flow and pre-requisites.

https://help.sap.com/viewer/3c4e8fc004cb4401a4fdd737f02ac2b9/7.5.6/en-
US/90d8fa4c8b38425aae560d1d402fe627.html

Creating OAuth2.0 client profile:

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 4/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

1.Create OAuth2.0 client profile from SE80 as below.


1. Start the object navigator (transaction SE80).
2. Choose Development Object in the dropdown list.
3. To create a development object in the SAP namespace, choose Create OAuth 2.0 Client Profile in the context menu
of the object name.
4. Enter the object name in the Client Profile field of the popup as ‘ZOAUTH_CLIENT_PROFILE’.
5. choose the type of service provider as ‘DEFAULT’
6. Also provide the scope as configured in the service provider configuration and activate the client profile.

7.

Configure the OAuth2.0 Client

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 5/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

1. Go to transaction OA2C_CONFIG to configure the OAuth2.0


2. Click on ‘Create’.
3. Select the OAuth2.0 Client Profile as ‘ZOAUTH_CLIENT_PROFILE’ and provide the Client ID.
4. Maintain the Client Secrete
5. Also provide the Token Endpoint.
6. Enter the Client Authentication as ‘Basic’, Resource Access Authentication as ‘Header Field’ and select grant type as
‘Client Credentials’.
7. Click on save. The OAuth2.0 configuration name is ‘ZOAUTH_CLIENT_PROFILE’

8.

Now the OAuth2.0 configuration is completed.

Create an ABAP program that uses OAuth 2.0 Client API:

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 6/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

OAuth 2.0 client is used together with the HTTP/REST client in our ABAP program. It sets an OAuth 2.0 token and makes
the HTTP or REST client send the token back to the program and receive it again.

The following image displays the process.

Process:

1. Create an instance of the OAuth 2.0 client type IF_OAUTH2_CLIENT.


2. Create an instance of the HTTP client type IF_HTTP_CLIENT.
Now, the OAuth 2.0 client instance is used to set the access token in the HTTP client.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 7/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

3. To trigger the access token, the application program calls the SET_TOKEN method in the OAuth 2.0 client instance
and sends the HTTP client instance as a parameter.
4. (a and b) After the access token was handed over to the HTTP client as described in step 3, use the HTTP client to
access OAuth 2.0 protected resources.

Below is the code sample:


Here populate the LV_URL with the API main endpoint. Also populate the method value as ‘GET’.

We can also create the RFC destination to maintain the Main API endpoint.

Here we will use the profile name and configuration name as ‘ZOAUTH_CLIENT_PROFILE’ to set the OAuth2.0 token.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 8/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Data: param_kind TYPE string VALUE ‘H’.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 9/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 10/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Get the HTTP status by calling the GET_STATUS method.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 11/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 12/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Conclusion:
Using OAuth2.0 configuration, we can call the OAuth2.0 enabled external service from ABAP program.

Additional Details:

In order to execute the program, the user should have the role assigned with auth. object S_OA2C_USE.

Also, the OAuth2.0 client profile is transportable to next environments.

The user who create OAUTH client configuration using t-code : OA2C_CONFIG should have a role assigned with the auth.
objects S_OA2C_ADM and S_SEC_COMM. This would be a manual configuration.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 13/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Alert Moderator
Assigned Tags

NW ABAP Gateway (OData)

ABAP Development

API

Similar Blog Posts 


ABAP acting as a Resource Server. App2App integration with OAuth2SAMLBearerAssertion flow.
By Piotr Tesny May 25, 2021

Blueprinting LOB extensions with SAP BTP services and API Management
By Piotr Tesny Jun 17, 2021

Principal propagation in a multi-cloud solution between Microsoft Azure and SAP Business Technology Platform (BTP), Part IV: SSO with a Power Virtual
Agents Chatbot and On-Premises Data Gateway
By Martin Raepple Apr 13, 2021

Related Questions 
How to test OAuth 2.0 enabled SAP OData service from POSTMAN Native application?
By Jagrut Patil May 29, 2018

Connect S/4 HANA OnPremise to SAP Cloud Platform using OAuth2SAMLBearerAssertion


By Piyush Kumar Sep 24, 2018

Authentication Popup when Trying to Use SAP OData Service

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 14/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

By Lexus Mans Aug 19, 2020

34 Comments

You must be Logged on to comment or reply to a post.

Henning Marquard
December 18, 2020 at 8:07 am

Thank you for the nice blog post, we need this technique to retrieve items from the Ariba API to integrate them into the SAP Fiori MyInbox

Like 1 | Share

Lakshmi Ganga
December 18, 2020 at 2:41 pm

Great I hope this helps!! Please do let me know if any issues.

Like 0 | Share

Husain Dahodwala
October 18, 2021 at 8:11 pm

Were you able to do this ? I am trying to achieve the same thing but i am getting an error while trying to call the ARIBA approval API after
getting the Oauth token.

Like 0 | Share

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 15/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Shashikant Wadhavane
March 2, 2021 at 1:04 pm

Thanks Laxmi for the blog. We have similar requirement to enable OAuth 2.0 for the service provider. We have followed the steps mentioned in the
above log but when we ABAP program, at the method 'lo_oa2c_client->set_token ', the exception is triggered when select from table
OA2C_TOKEN_ADM fails as no entry for SY-UNAME exists in table.

Error At: Error calling EXECUTE_REFRESH_FLOW.


Exception Message : No refresh token available for current user.

Any suggestion if any config is missed ?

Like 0 | Share

Lakshmi Ganga
March 2, 2021 at 3:15 pm

Hi,

As mentioned in the blog, please try using 'EXECUTE_CC_FLOW.`

If everything is correct, this should work. We have implemented this for both GET / POST calls.

TRY.

CALL METHOD lo_oa2c_client->set_token


EXPORTING
io_http_client = lo_http_client
i_param_kind = param_kind.

CATCH cx_oa2c INTO lx_oa2c.


TRY.
CALL METHOD lo_oa2c_client->execute_cc_flow.
CATCH cx_oa2c INTO lx_oa2c.
WRITE: `Error calling EXECUTE_CC_FLOW.`.
WRITE: / lx_oa2c->get_text( ).
https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 16/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

RETURN.
ENDTRY.
TRY.
CALL METHOD lo_oa2c_client->set_token
EXPORTING
io_http_client = lo_http_client
i_param_kind = param_kind.
CATCH cx_oa2c INTO lx_oa2c.
WRITE: `Error calling SET_TOKEN.`.
WRITE: / lx_oa2c->get_text( ).
RETURN.
ENDTRY.
ENDTRY.

Let me know if that solve the issue.

Like 0 | Share

Shashikant Wadhavane
March 2, 2021 at 3:41 pm

Yes Lakshmi. In the method call, there is direct selection from table and if entry not found it raises exception as shown in the image

CALL METHOD lo_oa2c_client->set_token


EXPORTING
io_http_client = lo_http_client
i_param_kind = param_kind.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 17/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Exception triggering Point

This exception is captured and new method EXECUTE_CC_FLOW is called. but this method also has same selection and it triggers
another exception.

In your system, does this table contain any permanent entries for some users ?

Like 0 | Share

Lakshmi Ganga | Blog Post Author


March 2, 2021 at 5:16 pm

Hi,

Initially, we faced the same selection failed.

But if EXECUTE_CC_FLOW failed means, the OAuth2.0 client configuration has some issue.

Could you please recheck.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 18/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Thanks,

Lakshmi

Like 0 | Share

Anuradha devi Adabala


July 29, 2022 at 11:48 am

Hi Lakshmi,

I have followed steps mentioned in the above Blog,I am not able to find method 'EXECUTE_CC_FLOW' in interface
'IF_OAUTH2_CLIENT'.Let me know if you can provide any other alternate solution.

Like 0 | Share

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 19/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Fabian Esteban Alvarez Pereira


July 16, 2021 at 4:31 am

Hi Lakshmi!!! it's very well post.

which user do you mean when you say:

The user who create OAUTH client configuration using t-code : OA2C_CONFIG should have a role assigned with the auth. objects S_OA2C_ADM and
S_SEC_COMM. This would be a manual configuration. ???

because i'm faced with the following error:

500 SAP Internal Server Error


ERROR: The calling program is not authorized to instantiate the internal OAuth 2.0 client (termination: RABAX_STATE)

Like 0 | Share

Vivek Gupta
September 29, 2021 at 6:51 am

Hello lakshmi,

Post is so wonderful.

while i am calling oa2c_grant tcode than its gives me configuration error in

44306/sap/bc/webdynpro/sap/OA2C_GRANT_APP?sap-
client=200&error=oa2c_error&error_description=Client%20configuration%20error%20or%20network%20problems.%20See%20kernel%20tra

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 20/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

ces.#

error=oa2c_error
error_description=Client%20configuration

please help

Like 1 | Share

Lakshmi Ganga | Blog Post Author


September 29, 2021 at 3:15 pm

Can you check /sap/bc/webdynpro/sap/OA2C_GRANT_APP is active in SICF. Based on the error messages, seems like a problem while
accessing the app ...so please verify that no network problems are causing issue

Like 1 | Share

Vivek Gupta
September 29, 2021 at 3:51 pm

service is activated but not imapact on status,


its red .

Like 1 | Share

Letissia Frikh
April 28, 2022 at 1:37 pm

Hello Vicek,

Did you solve the problem ?

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 21/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Help please ! i have the same error message when requesting OAth2 token.

thnx !

Like 0 | Share

Gunasekhar Kamasani
October 19, 2022 at 8:43 am

hi Vivek,

how did you solve service does not activate in OA2C_GRANT tcode

Like 0 | Share

Vivek Gupta
September 29, 2021 at 6:53 am

Help will be apprciated

Like 0 | Share

Husain Dahodwala
October 18, 2021 at 5:21 pm

Were you able to solve this? When I goto OA2C_GRANT I dont see any entry in the table.

OA2C_CONFIG has been done as shown above. Is there a way to validate if the config is correct?

Like 0 | Share

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 22/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Arjun Wadhawan
November 17, 2021 at 11:10 am

Hi Lakshmi,

I am using SAP ECC 6 EHP8 Sp11 to integrate with Azure APIM platform, I am not able to find the grant type as Client Credentials in tcode
OA2C_CONFIG.

Two grant type are visible i.e. Authorization Code and SAML 2.0 Bearer Assertion. Can you advice how to enable Client Credentials grant type ?

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 23/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

missing grant type

Regards,

Arjun

Like 0 | Share
https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 24/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Lars Hvam
January 18, 2022 at 1:57 pm

See SAP note 3041322 - OAuth 2.0 Client: Downport of grant type Client Credentials

Like 0 | Share

Wolfgang Fuchs
April 12, 2023 at 9:09 am

Hello Arjun,

could you get any further on this topic?

I'm curious, because i have some similar issues. We have 2 systems, S/4 Release 2020/FPS02 which works straigt away. but we also have
ECC SAP Netweaver 7.40 SP28 where we struggle.

Maybe you have some infos on that for me and of course all other ones here.
Would be great if you could share your experiences here.

Thanks a lot!

Wolfgang

Like 0 | Share

Arjun Wadhawan
April 14, 2023 at 9:55 pm

Hi Wolfgang,

First you need to upgrade SAP Kernel to 7.53 release

Secondly you need to install SAP Note 3113055 and execute the report.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 25/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Thirdly you need to install SAP Note 3041322 and then execute Tcode OA2C_CONFIG to see the client credential option.

Regards,

Arjun

Like 1 | Share

Murugesh Karunamurthy
January 27, 2022 at 5:48 am

with just the HTTP client methods , we are able to access external service providers.. (we get the token and then send this in the subsequent calls) .

what is the advantage of creating this Oauth client profiles? Could you please explain

Like 0 | Share

Wolfgang Karweger
April 17, 2023 at 3:20 pm

Hi Murugesh,

I´m complitly struggling with this issue. I have to setup a connection to an Screendragon API with OAuth 2.0. Because we are not on 7.50 I
cannot use the client profiles.

Can you please explain me your way with the HTTP client methods? For me this stuff is complitly new.

Thanks

Like 0 | Share
https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 26/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Nilesh Puranik
March 29, 2022 at 11:49 am

Hello Lakshmi,

Many thanks for this blog. Exactly what we were looking for.

You mentioned that it is 'Transportable' to the next environments. Can you explain how do we do that ?

Thanks in advance!

Regards,

Nilesh

Like 1 | Share

Letissia Frikh
April 28, 2022 at 1:43 pm

Hello,

Thanks for this blog.


I have a question plz. what is the difference if we choose Service Provider type "DEFAULT" or "HANA_CLOUD_PLATFORM".
( my auth server is external )

thnx !

Like 0 | Share
https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 27/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

ABAP Programmer
October 13, 2022 at 12:40 am

Hi Lakshmi,

Thanks for the blog.

Quick Question, using this approach, can we pass the Generated Token to a SOAP Proxy Call.

Regards,

Karthik

Like 0 | Share

P Sai
November 15, 2022 at 8:25 am

Hi

Is it Possible to use Oauth in case of SICF nodes. We have created SICF API service . Need to access it from external system .How to make it OAuth
enabled

Like 0 | Share

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 28/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Hakan Asikoglu
November 18, 2022 at 7:59 pm

Hello,

I am getting all the time below error message. It is very complicated to configure thousand steps and in the end no seccess!

Error calling EXECUTE_REFRESH_FLOW.


No refresh token available for current user.

Furthermore this method hasn't a parameter i_configuration, just only i_profile, thus what are you showing there? Please see above your code
description

TRY.

CALL METHOD cl_oauth2_client=>create


EXPORTING
i_profile = profile
RECEIVING
ro_oauth2_client = lo_oa2c_client.

CATCH cx_oa2c INTO lx_oa2c.


WRITE: `Error calling CREATE.`.
WRITE: / lx_oa2c->get_text( ).
RETURN.
ENDTRY.

Like 0 | Share

Wolfgang Fuchs
April 12, 2023 at 9:07 am

Hi Hakan,

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 29/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

could you get any further on this topic?

I'm curious, because i have some similar issues. We have 2 systems, S/4 Release 2020/FPS02 which works straigt away. but we also have
ECC SAP Netweaver 7.40 SP28 where we struggle.

Maybe you have some infos on that for me and of course all other ones here.
Would be great if you could share your experiences here.

Thanks a lot!

Wolfgang

Like 0 | Share

Hakan Asikoglu
April 14, 2023 at 8:07 am

Hello Wolfgang,

unfortunately not.

I solved it programmatically..

https://github.com/gregorwolf/abapOAuthAzure/blob/master/zmsazure.prog.abap

Br

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 30/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Hakan

Like 1 | Share

Geert Nackers
December 31, 2022 at 2:52 pm

Hello,

Thanks for this blog.

Do you have done this also when the Grant Type = "Authorization code"? Meaning first getting a code in order to be able to get the token via a
second call with this code. What would be the additional steps in that case?

Kind regards,

Geert

Like 0 | Share

swarnali basu
January 31, 2023 at 7:35 pm

Hi Lakshmi

thanks for the blog however I need a help .

I am using the option password credentials instead of client credentials and I have configured likewise , once the give the user name and password I
get error as below

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 31/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

“ the error is not defined in the oauth2.0 specification “

but the samne id /pswd works in postman. Please suggest what is the issue here

Like 0 | Share

Julia Diemand
February 27, 2023 at 9:10 am

Hello,

thanks for the blog. I have followed every step of the blog, but when i try to run the program, i get the following error message:

Error calling EXECUTE_CC_FLOW.

HTTP error, failed processing, invalid state, invalid timeout, or other reasons.

In postman, however, i was able to call my workflow without any problems.

Do you have any ideas how to solve this issue?

Kind regards,

Julia

Like 0 | Share

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 32/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

Sandra Rossi
March 30, 2023 at 7:18 pm

To help future visitors, in case they don't appreciate typing code manually, below is the code snippets from the blog post (please fix any typo).

** Create the HTTP client


CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
ssl_id = 'ANONYM'
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin not active = 2
Internal = 3
OTHERS = 4.

* Turn off I ogon popup. Detect authentication errors.


lo_http_client->propertytype_logon_popup = 0.

CALL METHOD lo_http_client->request->set_method


EXPORTING
method = method.

* Set OAuth 2.0 Token


TRY.
CALL METHOD cl_oauth2_client=>create
EXPORTING
i_profile = profile
i_configuration = xxxxxxxxxxxxxxxx
RECEIVING
ro_oauth2_client = DATA(lo_oa2c_client)
https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 33/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

CATCH cx_oa2c INT0 DATA(lx_oa2c) .


WRITE : `Error calling CREATE.`.
WRITE : / lx_oa2c->get_text( ).
RETURN.
ENDTRY .

Data: param_kind TYPE string VALUE 'H'.

TRY.

CALL METHOD lo_oa2c_client->set_token


EXPORTING
io_http_client = lo_http_client
param_kind = param_kind.

CATCH cx_oa2c INT0 1x_oa2c.


TRY.
CALL METHOD lo_oa2c_client->execute_cc_flow .
CATCH cx_oa2c INT0 1x_oa2c.
WRITE: `Error calling EXECUTE_CC_FLOW.`.
WRITE: / 1x_oa2c->get_text( ).
RETURN.
ENDTRY.
TRY.
CALL METHOD lo_oa2c_client->set_token
EXPORTING
io_http_client = lo_http_client
param_kind = param_kind.
CATCH cx_oa2c INT0 1x_oa2c.
WRITE: `Error calling SET_TOKEN.`.
WRITE: / 1x_oa2c->get_text( ).
RETURN.
https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 34/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

ENDTRY.
ENDTRY.

* Send / Receive Request


CALL METHOD lo_http_client->send
EXCEPTIONS
http_communication_failure = 1
http_Invalid_state = 2
http_processing_failed = 3
http_Invalid_timeout = 4
OTHERS = 5.
IF sy—subrc <> 0.
MESSAGE ID sy-msgid TYPE sy—msgty NUMBER sy—msgno
WITH sy—msgvl sy—msgv2 sy—msgv3 sy—msgv4.
ENDIF.

CALL METHOD lo_http_client->receive


EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
IF sy—subrc <> 0.
MESSAGE ID sy-msgid TYPE sy—msgty NUMBER sy—msgno
WITH sy—msgvl sy—msgv2 sy—msgv3 sy—msgv4.
ENDIF.

* Display result
CALL METHOD lo_http_client->get_status
IMPORTING
code = l_status_code.
WRITE / |{ l_status_code }|.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 35/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

WRITE /.

IF l_status_code = 200.
CALL METHOD lo_http_client->response->get_cdata
RECEIVING
data = l_response_data.

DATA(l_content_type) = lo_http_client->response->get_content_type( ).
IF l_content_type CP `text/html*`.
cl_demo_output=>display_html( html = l_response_data ).
ELSEIF l_content_type CP `text/xml*`.
cl_demo_output=>display_xml( xml = l_response_data ).
ELSEIF l_content_type CP `application/json*`.
cl_demo_output=>display_json( json = l_response_data ).
ENDIF.
ELSE.
CALL METHOD lo_http_client->response->get_header_fields
CHANGING
fields = lt_fields.

LOOP AT lt_fields ASSIGNING FIELD-SYMBOL(<ls_fie1d>).


WRITE: / <ls_fie1d>-name, 25 <ls_fie1d>-value.
ENDLOOP.
ENDIF.

* close
CALL METHOD lo_http_client->close
EXCEPTIONS
http_invalid_state = 1
OTHERS = 2.
IF sy—subrc <> 0.
MESSAGE ID sy-msgid TYPE sy—msgty NUMBER sy—msgno

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 36/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

WITH sy—msgvl sy—msgv2 sy—msgv3 sy—msgv4.


ENDIF.

Like 0 | Share

Nihar Jain
May 3, 2023 at 1:57 pm

Hello,

Thanks a lot for the blog.

While trying it out i observed that there are some new methods which we can directly use instead of using the setToken method of lo_oa2_client.
You guys can check my code if that helps you.

data: client type ref to if_http_client,

l_http_client type timestampl,

l_http_start type timestampl,

l_http_end type timestampl,

lv_body TYPE string..

get time stamp field l_http_client.

call method cl_http_client=>create_by_url

exporting

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 37/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

url = 'Your URL here'

importing

client = client

exceptions

argument_not_found = 1

internal_error =2

plugin_not_active = 3

others = 4.

cl_http_client=>set_oauth_token(

EXPORTING

io_http_client = client

i_oauth_config = 'ZOAUTH_CLIENT_PROFILE_NJ'

i_oauth_profile = 'ZOAUTH_CLIENT_PROFILE_NJ'

).

lv_body = '{ "userUUID" : "abcd", "teamCategory": "abcd"}'.

client->request->set_content_type( content_type = if_rest_media_type=>gc_appl_json ).

client->request->set_cdata( data = lv_body ).

get time stamp field l_http_start.

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 38/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

client->send(

exceptions

http_communication_failure = 1 " Communication Error

http_invalid_state =2 " Invalid state

http_processing_failed =3 " Error when processing method

http_invalid_timeout =4 " Invalid Time Entry

others =5

).

if sy-subrc <> 0.

call method client->get_last_error

importing

code = data(subrc)

message = data(errortext).

write: / 'communication_error( send )',

/ 'code: ', subrc, 'message: ', errortext.

exit.

endif.

client->receive(

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 39/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

exceptions

http_communication_failure = 1 " Communication Error

http_invalid_state =2 " Invalid state

http_processing_failed =3 " Error when processing method

others =4

).

if sy-subrc <> 0.

call method client->get_last_error

importing

code = subrc

message = errortext.

write: / 'communication_error( send )',

/ 'code: ', subrc, 'message: ', errortext.

exit.

endif.

client->response->get_cdata(

receiving

data = data(response) " Character data

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 40/41
5/6/23, 17:21 Configuring OAuth 2.0 and Creating an ABAP Program That Uses OAuth 2.0 Client API | SAP Blogs

).

get time stamp field l_http_end.

write: 'received response:', response.

write: /'http client creation: ', l_http_client,

/'http start time: ', l_http_start,

/'http end time: ', l_http_end,

/'sql end time: ', l_sql_end.

exit.

Like 0 | Share

Find us on

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Preferencias sobre cookies

Newsletter Support

https://blogs.sap.com/2020/12/18/configuring-oauth-2.0-and-creating-an-abap-program-that-uses-oauth-2.0-client-api/ 41/41

You might also like