The post is a response to the lack of standard mechanisms for performing backups in SAP SAC at the model level by the user.
The inspiration was one of the scenarios considered by Max_Gander in the post:
Options to Back-up Your SAP Analytics Cloud Planni… – SAP Community
In my organization, I used the scenario Back-up model in SAP Analytics Cloud for one of the models. The reason was the longer time needed to develop an ABAP program that could call an import package in Content Network from my favorite SAP BW/4.
Below I will present the code and configuration that will allow you to call the import job in content management without using third-party tools like postman etc.
So it’s an implementation of Content Network backup scenario from the blog mentioned
1. Set Up Third-Party Access with OAuth Clients
This is the first step, which is best done according to the documentation.
Set Up Third-Party Access with OAuth Clients | SAP Help Portal
Briefly, SAC -> System->Administration->App Integration-> +Add an OAuth Client
When creating an OAuth client, the system displays the secret key only once, so don’t forget to copy it
2. Set up connection in SAP BW/4
SM59 -> HTTP Connection to External Server
Â
Test it
Â
3. Program with all important comments
REPORT REPSACAPI.
DATA: LO_HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT,
lo_API02 TYPE REF TO IF_HTTP_CLIENT,
lo_API02a TYPE REF TO IF_HTTP_CLIENT.
DATA: response TYPE string,
http_status type string,
lv_code type i,
reason type string.
data: lv_url type string.
TYPES: BEGIN OF ty_token_response,
access_token TYPE string,
token_type TYPE string,
expires_in TYPE i,
scope TYPE string,
jti TYPE string,
END OF ty_token_response.
DATA: ls_token_data TYPE ty_token_response,
lv_token TYPE string,
lv_atok type string,
lv_csrf_token TYPE string,
lv_body TYPE string.
DATA: lt_headers TYPE tihttpnvp,
ls_header TYPE ihttpnvp,
lt_headers01 type tihttpnvp.
DATA: lv_content_length TYPE string.
DATA: lt_cookies TYPE TABLE OF string,
lv_cookie TYPE string,
lv_cookie_remaining TYPE string.
DATA lv_decoded_cookie TYPE string.
*Put your SAC hosi into lv_url_d
PARAMETERS: lv_url_d type string OBLIGATORY LOWER CASE.
******Begin: to get <Access_Token> *****
CL_HTTP_CLIENT=>CREATE_BY_DESTINATION(
EXPORTING
DESTINATION = ‘Your RFC name’ “Name of the RFC destination
IMPORTING
CLIENT = LO_HTTP_CLIENT ” HTTP Client object
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
DESTINATION_NOT_FOUND = 2
DESTINATION_NO_AUTHORITY = 3
PLUGIN_NOT_ACTIVE = 4
INTERNAL_ERROR = 5
OTHERS = 6 ).
lo_http_client->request->set_method( if_http_request=>CO_REQUEST_METHOD_POST ).
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.
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 5.
ENDIF.
response = lo_http_client->response->get_cdata( ).
CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = response
CHANGING
data = ls_token_data.
******End: to get <Access_Token> *****
******Begin: to get X-CSRF token *****
lv_url = LV_URL_D && ‘/api/v1/csrf’.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lo_API02
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
lo_API02->request->set_method(‘GET’).
lv_atok = |Bearer { ls_token_data-access_token }|.
lo_API02->request->set_header_field( name = ‘Authorization’ value = lv_atok ).
lo_API02->request->set_header_field( name = ‘x-csrf-token’ value = ‘fetch’ ).
lo_API02->request->set_header_field( name = ‘x-sap-sac-custom-auth’ value = ‘true’ ).
lo_API02->request->set_header_field( name = ‘DataServiceVersion’ value = ‘2.0’ ).
lo_API02->request->set_header_field( name = ‘Accept’ value = ‘application/json’ ).
CALL METHOD lo_API02->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
IF sy-subrc = 0.
CALL METHOD lo_API02->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 5.
ENDIF.
call method lo_API02->response->get_status
IMPORTING
code = lv_code
REASON = reason.
CALL METHOD lo_API02->response->get_header_field
EXPORTING
name = ‘x-csrf-token’
RECEIVING
value = lv_csrf_token.
CALL METHOD lo_api02->response->get_header_fields
CHANGING
fields = lt_headers.
response = lo_API02->response->get_cdata( ).
******End: to get X-CSRF token *****
******Begin: run the job
lv_url = LV_URL_D && ‘/api/v1/content/jobs’.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lo_API02a
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
lo_API02a->request->set_method(‘POST’).
* !!! Crucial: pass cookies
LOOP AT lt_headers INTO ls_header.
IF ls_header-name = ‘set-cookie’.
IF ls_header-value CS ‘JSESSIONID=’.
SPLIT ls_header-value AT ‘;’ INTO lv_cookie lv_cookie_remaining.
lv_cookie = lv_cookie+11.
EXIT.
ENDIF.
ENDIF.
ENDLOOP.
lv_decoded_cookie = cl_http_utility=>unescape_url(
escaped = lv_cookie ).
lv_body = ‘{“type”: “IMPORT”, “packageName”: “Your Package name”,”privatePackage”: Your Package ID,”allObjects”: true}’.
lo_API02a->request->set_header_field( name = ‘Authorization’ value = lv_atok ).
lo_API02a->request->set_header_field( name = ‘x-sap-sac-custom-auth’ value = ‘true’ ).
lo_API02a->request->set_header_field( name = ‘Connection’ value = ‘keep-alive’ ).
lo_API02a->request->set_header_field( name = ‘Content-Type’ value = ‘application/json’ ).
lo_API02a->request->set_header_field( name = ‘x-csrf-token’ value = lv_csrf_token ).
lo_API02a->request->set_cookie( name = ‘JSESSIONID’ value = lv_decoded_cookie ).
lo_API02a->request->set_header_field( name = ‘X-Requested-With’ value = ‘XMLHttpRequest’ ).
lv_content_length = strlen( lv_body ).
lo_API02a->request->set_header_field( name = ‘Content-Length’ value = lv_content_length ).
lo_API02a->request->set_cdata( lv_body ).
CALL METHOD lo_API02a->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
IF sy-subrc = 0.
CALL METHOD lo_API02a->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 5.
ENDIF.
call method lo_API02a->response->get_status
IMPORTING
code = lv_code
REASON = reason.
response = lo_API02a->response->get_cdata( ).
CALL METHOD lo_API02a->close.
CALL METHOD lo_API02->close.
CALL METHOD LO_HTTP_CLIENT->close.
******End: run the job
WRITE: ‘response: ‘, response.
Â
 4. Finally you can check the result via SAC-> Transport-> Monitor
Â
Sources:
Introduction of SAC Multi-actions Public API – SAP Community
Â
​ The post is a response to the lack of standard mechanisms for performing backups in SAP SAC at the model level by the user.The inspiration was one of the scenarios considered by Max_Gander in the post:Options to Back-up Your SAP Analytics Cloud Planni… – SAP CommunityIn my organization, I used the scenario Back-up model in SAP Analytics Cloud for one of the models. The reason was the longer time needed to develop an ABAP program that could call an import package in Content Network from my favorite SAP BW/4.Below I will present the code and configuration that will allow you to call the import job in content management without using third-party tools like postman etc.So it’s an implementation of Content Network backup scenario from the blog mentioned1. Set Up Third-Party Access with OAuth ClientsThis is the first step, which is best done according to the documentation.Set Up Third-Party Access with OAuth Clients | SAP Help PortalBriefly, SAC -> System->Administration->App Integration-> +Add an OAuth ClientWhen creating an OAuth client, the system displays the secret key only once, so don’t forget to copy it2. Set up connection in SAP BW/4SM59 -> HTTP Connection to External Server Test it 3. Program with all important commentsREPORT REPSACAPI.
DATA: LO_HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT,
lo_API02 TYPE REF TO IF_HTTP_CLIENT,
lo_API02a TYPE REF TO IF_HTTP_CLIENT.
DATA: response TYPE string,
http_status type string,
lv_code type i,
reason type string.
data: lv_url type string.
TYPES: BEGIN OF ty_token_response,
access_token TYPE string,
token_type TYPE string,
expires_in TYPE i,
scope TYPE string,
jti TYPE string,
END OF ty_token_response.
DATA: ls_token_data TYPE ty_token_response,
lv_token TYPE string,
lv_atok type string,
lv_csrf_token TYPE string,
lv_body TYPE string.
DATA: lt_headers TYPE tihttpnvp,
ls_header TYPE ihttpnvp,
lt_headers01 type tihttpnvp.
DATA: lv_content_length TYPE string.
DATA: lt_cookies TYPE TABLE OF string,
lv_cookie TYPE string,
lv_cookie_remaining TYPE string.
DATA lv_decoded_cookie TYPE string.
*Put your SAC hosi into lv_url_d
PARAMETERS: lv_url_d type string OBLIGATORY LOWER CASE.
******Begin: to get <Access_Token> *****
CL_HTTP_CLIENT=>CREATE_BY_DESTINATION(
EXPORTING
DESTINATION = ‘Your RFC name’ “Name of the RFC destination
IMPORTING
CLIENT = LO_HTTP_CLIENT ” HTTP Client object
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
DESTINATION_NOT_FOUND = 2
DESTINATION_NO_AUTHORITY = 3
PLUGIN_NOT_ACTIVE = 4
INTERNAL_ERROR = 5
OTHERS = 6 ).
lo_http_client->request->set_method( if_http_request=>CO_REQUEST_METHOD_POST ).
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.
CALL METHOD lo_http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 5.
ENDIF.
response = lo_http_client->response->get_cdata( ).
CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = response
CHANGING
data = ls_token_data.
******End: to get <Access_Token> *****
******Begin: to get X-CSRF token *****
lv_url = LV_URL_D && ‘/api/v1/csrf’.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lo_API02
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
lo_API02->request->set_method(‘GET’).
lv_atok = |Bearer { ls_token_data-access_token }|.
lo_API02->request->set_header_field( name = ‘Authorization’ value = lv_atok ).
lo_API02->request->set_header_field( name = ‘x-csrf-token’ value = ‘fetch’ ).
lo_API02->request->set_header_field( name = ‘x-sap-sac-custom-auth’ value = ‘true’ ).
lo_API02->request->set_header_field( name = ‘DataServiceVersion’ value = ‘2.0’ ).
lo_API02->request->set_header_field( name = ‘Accept’ value = ‘application/json’ ).
CALL METHOD lo_API02->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
IF sy-subrc = 0.
CALL METHOD lo_API02->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 5.
ENDIF.
call method lo_API02->response->get_status
IMPORTING
code = lv_code
REASON = reason.
CALL METHOD lo_API02->response->get_header_field
EXPORTING
name = ‘x-csrf-token’
RECEIVING
value = lv_csrf_token.
CALL METHOD lo_api02->response->get_header_fields
CHANGING
fields = lt_headers.
response = lo_API02->response->get_cdata( ).
******End: to get X-CSRF token *****
******Begin: run the job
lv_url = LV_URL_D && ‘/api/v1/content/jobs’.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_url
IMPORTING
client = lo_API02a
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
lo_API02a->request->set_method(‘POST’).
* !!! Crucial: pass cookies
LOOP AT lt_headers INTO ls_header.
IF ls_header-name = ‘set-cookie’.
IF ls_header-value CS ‘JSESSIONID=’.
SPLIT ls_header-value AT ‘;’ INTO lv_cookie lv_cookie_remaining.
lv_cookie = lv_cookie+11.
EXIT.
ENDIF.
ENDIF.
ENDLOOP.
lv_decoded_cookie = cl_http_utility=>unescape_url(
escaped = lv_cookie ).
lv_body = ‘{“type”: “IMPORT”, “packageName”: “Your Package name”,”privatePackage”: Your Package ID,”allObjects”: true}’.
lo_API02a->request->set_header_field( name = ‘Authorization’ value = lv_atok ).
lo_API02a->request->set_header_field( name = ‘x-sap-sac-custom-auth’ value = ‘true’ ).
lo_API02a->request->set_header_field( name = ‘Connection’ value = ‘keep-alive’ ).
lo_API02a->request->set_header_field( name = ‘Content-Type’ value = ‘application/json’ ).
lo_API02a->request->set_header_field( name = ‘x-csrf-token’ value = lv_csrf_token ).
lo_API02a->request->set_cookie( name = ‘JSESSIONID’ value = lv_decoded_cookie ).
lo_API02a->request->set_header_field( name = ‘X-Requested-With’ value = ‘XMLHttpRequest’ ).
lv_content_length = strlen( lv_body ).
lo_API02a->request->set_header_field( name = ‘Content-Length’ value = lv_content_length ).
lo_API02a->request->set_cdata( lv_body ).
CALL METHOD lo_API02a->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
IF sy-subrc = 0.
CALL METHOD lo_API02a->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 5.
ENDIF.
call method lo_API02a->response->get_status
IMPORTING
code = lv_code
REASON = reason.
response = lo_API02a->response->get_cdata( ).
CALL METHOD lo_API02a->close.
CALL METHOD lo_API02->close.
CALL METHOD LO_HTTP_CLIENT->close.
******End: run the job
WRITE: ‘response: ‘, response.  4. Finally you can check the result via SAC-> Transport-> Monitor Sources:Introduction of SAC Multi-actions Public API – SAP Community   Read More Technology Blog Posts by Members articlesÂ
#SAP
#SAPTechnologyblog