0002 Zu Consume Netsuite API Oauth2.0
0002 Zu Consume Netsuite API Oauth2.0
0002 Zu Consume Netsuite API Oauth2.0
*---------------------------------------------------------------------*
* Global DATA Declaration *
*---------------------------------------------------------------------*
DATA:
o_http_client TYPE REF TO if_http_client,
gv_access_token TYPE string,
gv_http_status_code TYPE i,
gv_http_status_text TYPE string,
gs_response TYPE string,
gs_raw_response TYPE string,
gv_header_attribute_name TYPE string,
gv_header_attribute_value TYPE string.
CONSTANTS:
gc_cookie TYPE string VALUE 'Cookie',
gc_cookie_value TYPE string VALUE 'NS_ROUTING_VERSION=LEADING',
gc_content_type TYPE string VALUE 'Content-Type',
gc_authorization TYPE string VALUE 'Authorization',
gc_app_form TYPE string VALUE 'application/x-www-form-urlencoded',
gc_app_json TYPE string VALUE 'application/json',
gc_post TYPE string VALUE 'POST'.
*---------------------------------------------------------------------*
* SELECTION SCREEN *
*---------------------------------------------------------------------*
PARAMETERS: p_code TYPE string LOWER CASE.
*--Post Method
PERFORM set_post_method.
*--Authenticate
PERFORM authenticate_netsuite.
* Get Status
PERFORM get_response_status.
**************
SKIP 1.
WRITE:/ 'Step 2: Netsuit Token Return Code/Text : ', gv_http_status_code, '/',
gv_http_status_text.
SKIP 1.
*--Set Header
* Set Content Type app/json
PERFORM set_header_field USING gc_content_type
gc_app_json.
*--Prepare Body
PERFORM prepare_api_body.
* Get Response
PERFORM get_response_status.
*&---------------------------------------------------------------------*
*& Form call_auth_token_url
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM call_auth_token_url .
*&---------------------------------------------------------------------*
*& Form set_body_with_code
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM set_body_with_code USING p_code.
DATA:
lv_cdata TYPE string,
lv_content_length_value TYPE i,
lv_client_id TYPE tvarv_val,
lv_client_secret TYPE tvarv_val.
* Set Data
CALL METHOD o_http_client->request->if_http_entity~set_cdata
EXPORTING
data = lv_cdata
length = lv_content_length_value
offset = 0.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_SEND_METHOD
*&---------------------------------------------------------------------*
* SEND Method
*----------------------------------------------------------------------*
FORM set_send_method .
* Get Status
CLEAR: gv_http_status_code,
gv_http_status_text,
gs_response.
gs_response = o_http_client->response->get_cdata( ).
gs_raw_response = o_http_client->response->get_raw_message( ).
*&---------------------------------------------------------------------*
*& Form READ_API_TOKEN_KEY
*&---------------------------------------------------------------------*
* Read the Token from the Response
*----------------------------------------------------------------------*
FORM read_api_token_key .
DATA:
lv_xml_result_str TYPE string,
lv_xml_result_xstr TYPE xstring.
* Write the data received back from the POST to the screen:
CLEAR lv_xml_result_str.
lv_xml_result_str = o_http_client->response->get_cdata( ).
FIELD-SYMBOLS: <fs>.
LOOP AT itab_result.
CASE itab_result-str.
WHEN 'access_token'.
lgv_access_token_str_found = abap_true.
WHEN 'expires_in'.
lv_expire_str_found = abap_true.
WHEN OTHERS.
IF lgv_access_token_str_found EQ abap_true AND gv_access_token IS INITIAL.
w_strlen = strlen( itab_result-str ).
IF w_strlen > 30.
gv_access_token = itab_result-str.
ENDIF.
ENDIF.
*&---------------------------------------------------------------------*
*& Form SET_HEADER_FIELD
*&---------------------------------------------------------------------*
* Set Header Field
*----------------------------------------------------------------------*
FORM set_header_field CHANGING p_name
p_value.
o_http_client->request->set_header_field(
name = p_name
value = p_value
).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form authenticate_netsuite
*&---------------------------------------------------------------------*
*& Put Client Id & Secret
*&---------------------------------------------------------------------*
FORM authenticate_netsuite .
* Get Client Id
SELECT SINGLE low FROM tvarvc INTO lv_client_secret
WHERE name = 'NETSUITE_CLIENT_SECRET'
AND type = 'P'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form prepare_api_body
*&---------------------------------------------------------------------*
*& Prepare JSON Body
*&---------------------------------------------------------------------*
FORM prepare_api_body .
* You will have to put your logic to get the data from SAP Tables and Serialize
ABAP Data to JSON.
* You may also need to remove and add some characters as per NetSuite Requirement.
* For Demo Purpose, we have hardcoded the JSON data.
lv_json_c = '{ "custrecord_paer_edi_type": "8500", "custrecord_paer_req_type":
"Sales Order Import", "custrecord_paer_rowstate": { "id":
"6" },"custrecord_paer_object":"{ \"name\": \"JS\", \"age\": 32, \"city\": \"New
York\" }" }'.