Hello all,
I am attempting to do an HTTP Post from SAP using ABAP.
The exact same POST works fine from outside of SAP (e.g. using POSTMan).
In SAP, we get an unwanted Pop-Up Window which asks us for user id and password:
No matter what user Id and Password I enter I get an HTTP error 401 (Unauthorized).
I know I can disable this pop-up using the following code:
http_client->propertytype_logon_popup = http_client->co_disabled.
...however, we still get an HTTP error 401 (Unauthorized) if we disable the pop-up.
Question:
What is this Pop-Up really asking for?
I tried entering my SAP user id and Password and that did not work.
I tried entering the Google user id and password and that did not work either.
In fact we are supplying all of the authentication information in the HTTP POST so I should get no Pop-Up asking for user id and password.
Here is my code (I have made the Google Access Key and Access Token junk data for security reasons....) :
PROGRAM ZISUT_GOOGLE_HTTP_POST
LINE-SIZE 500 " Page width
LINE-COUNT 65 " Page height
NO STANDARD PAGE HEADING
MESSAGE-ID ZR.
************************************************************************
* Program: ZISUT_GOOGLE_HTTP_POST *
* Author : Chris Twirbutt *
* *
* Type : Online *
* Purpose: HTTP POST from SAP to Google API to insert record into *
* Google's Fusion tables. *
************************************************************************
* Change history *
*----------------------------------------------------------------------*
*UserID |Date |Change request & Reason for change *
*----------|----------|------------------------------------------------*
*TWIRBUTTC |09/10/2013|DPWK913900 - SP332- Created program *
* | | *
*----------|----------|------------------------------------------------*
************************************************************************
************************************************************************
* Objects/Classes/Interfaces, etc:
DATA: http_client TYPE REF TO if_http_client .
************************************************************************
* Structures:
DATA: W_HTTP_ERROR_DESCR TYPE STRING,
W_HTTP_ERROR_DESCR_LONG TYPE XSTRING.
DATA: W_HTTP_RETURN_CODE TYPE I.
************************************************************************
* Variables:
DATA: W_URL TYPE string,
W_XML_RESULT_STR TYPE string.
DATA: L_XML_RESULT_XSTR TYPE XSTRING.
************************************************************************
* Selection Screen:
SELECTION-SCREEN BEGIN OF BLOCK MAIN WITH FRAME TITLE TEXT-S01.
PARAMETERS: P_URL LIKE W_URL OBLIGATORY lower case.
SELECTION-SCREEN END OF BLOCK MAIN.
INITIALIZATION.
CONCATENATE 'https://www.googleapis.com/fusiontables/v1/query?' " GOOGLE API URL
'sql=INSERT%20INTO%201CqwRGEEn4L0gN66JwGvCR5yOI8miNMVijcp4XlE%20(Name,%20Age)%20VALUES%20(\''Fred\'',%2034)' " GOOGLE API SQL INSERT STATEMENT
* Note: Content Length = 0 since we are not putting anything in the Body of the HTTP Post,
* we are putting it all in the URL:
'&Content-length:=0' " HTTP POST CONTENT LENGTH (BODY)
'&Content-type:=application/json' " HTTP POST CONTENT TYPE
'&key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' " GOOGLE API ACCESS KEY
'&access_token=ya29.XXXXXXXXXXXXXXXXXXXXXXXXXX' " GOOGLE API ACCESS TOKEN
INTO P_URL.
************************************************************************
* Main Program Logic/Flow:
START-OF-SELECTION .
* Create the HTTP client using the Google API URL:
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = P_URL
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Set up the HTTP Post to Google's API URL:
CALL METHOD http_client->request->set_method
EXPORTING
method = 'POST'.
CALL METHOD http_client->request->set_version
EXPORTING
* version = '1001'.
version = if_http_request=>co_protocol_version_1_1. " 1001
http_client->request->set_header_field(
name = 'Host'
value = 'www.googleapis.com'
).
* Send the HTTP Post to the URL / Google's API:
CALL METHOD http_client->send
EXPORTING
timeout = 15 " 15 Seconds
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2.
* We are getting an SAP POP-UP for User Id and Password. WHY ?????
* If I disable the pop-up we get an HTTP error (401 /Unauthorized).
* http_client->propertytype_logon_popup = http_client->co_disabled. " POPUP for user id and pwd
*Read the Response from Google:
CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
* Get the HTTP return code from the HTTP POST:
http_client->response->get_status( IMPORTING code = W_HTTP_RETURN_CODE ).
http_client->response->get_status( IMPORTING reason = W_HTTP_ERROR_DESCR ).
* http_client->response->GET_RAW_MESSAGE( IMPORTING data = W_HTTP_ERROR_DESCR_LONG ).
W_HTTP_ERROR_DESCR_LONG = http_client->response->GET_RAW_MESSAGE( ).
* Write the HTTP Return Code / Description to the screen:
WRITE:/ 'HTTP return Code/Description:', W_HTTP_RETURN_CODE no-gap, '/' no-gap, W_HTTP_ERROR_DESCR.
skip 1.
* Refresh the Request after each POST:
CALL METHOD http_client->refresh_request(
EXCEPTIONS
http_action_failed = 1
others = 2 ).
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Write the data received back from the POST to the screen:
CLEAR W_XML_RESULT_STR.
W_XML_RESULT_STR = http_client->response->get_cdata( ).
sy-tmaxl = strlen( W_XML_RESULT_STR ).
FIELD-SYMBOLS: <FS>.
ASSIGN W_XML_RESULT_STR TO <FS>.
WRITE:/ <FS>(SY-TMAXL).
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Check if we got back an HTTP Error:
IF W_HTTP_RETURN_CODE EQ 200. " ok
* Close the HTTP connection:
http_client->close( ).
ELSE.
MESSAGE S005(ZR) WITH 'Error: Return code = ' W_HTTP_RETURN_CODE.
ENDIF.
END-OF-SELECTION.