Skip to content
Uwe Fetzer edited this page Sep 5, 2013 · 3 revisions

Usage of ZOAuth

Read requests

Example of a SAP Streamwork read request (read all open activities).

*--- Streamwork -----------------------------------------------------*
DATA: oauth TYPE REF TO zcl_oauth
	, oauth_error   	TYPE REF TO zcx_oauth_error
	, error_text    	TYPE string
	, consumer_secret   TYPE string
	, token_secret  	TYPE string
	, secret        	TYPE string
	, parameters    	TYPE zoauth_key_value_t
	, response_data 	TYPE string
	.
FIELD-SYMBOLS: <parameter> TYPE LINE OF zoauth_key_value_t.
TRY .
	oauth = zcl_oauth=>get_instance( consumer_name = 'STREAMWORK' screen_name = 'UWE' ).
  CATCH zcx_oauth_error INTO oauth_error.
	error_text = oauth_error->get_text( ).
	MESSAGE error_text TYPE 'E'.
ENDTRY.

*--- create new secret ---*
consumer_secret = oauth->get_consumer_secret( ).
token_secret	= oauth->get_oauth_token_secret( ).
CONCATENATE
  consumer_secret
  token_secret
INTO secret.

*--- add plaintext signature ---*
INSERT INITIAL LINE INTO TABLE parameters ASSIGNING <parameter>.
<parameter>-key = 'oauth_signature'.
<parameter>-value = oauth->percent_encode( secret ).
oauth->set_parameters( parameters ).
oauth->append_oauth_parameters( ).
TRY .
	response_data = oauth->fetchurl( url = '/v1/activities' method = 'GET' accept_xml = 'X' ).
  CATCH zcx_oauth_error INTO oauth_error.
	error_text = oauth_error->get_text( ).
	MESSAGE error_text TYPE 'E'.
ENDTRY.

Write requests

Example of a SAP Streamwork write request (post a new text item to a given activity).

DATA: oauth TYPE REF TO zcl_oauth
	, oauth_error   	TYPE REF TO zcx_oauth_error
	, error_text    	TYPE string
	, consumer_secret   TYPE string
	, token_secret  	TYPE string
	, secret        	TYPE string
	, parameters    	TYPE zoauth_key_value_t
	, response_data 	TYPE string
	, cdata         	TYPE string
	.
FIELD-SYMBOLS: <parameter> TYPE LINE OF zoauth_key_value_t.
TRY .
	oauth = zcl_oauth=>get_instance( consumer_name = 'STREAMWORK' screen_name = 'UWE' ).
  CATCH zcx_oauth_error INTO oauth_error.
	error_text = oauth_error->get_text( ).
	MESSAGE error_text TYPE 'E'.
ENDTRY.

*--- create new secret ---*
consumer_secret = oauth->get_consumer_secret( ).
token_secret	= oauth->get_oauth_token_secret( ).
CONCATENATE
  consumer_secret
  token_secret
INTO secret.

*--- add plaintext signature ---*
INSERT INITIAL LINE INTO TABLE parameters ASSIGNING <parameter>.
<parameter>-key = 'oauth_signature'.
<parameter>-value = oauth->percent_encode( secret ).
oauth->set_parameters( parameters ).
oauth->append_oauth_parameters( ).
CONCATENATE
'<?xml version="1.0" encoding="UTF-8"?>'
'<item name="New sample text item using #twibap OAuth class">'
'  <text_item>'
'	<text_content>This is our test text_content.</text_content>'
'  </text_item>'
'</item>'
INTO cdata.

TRY .
	response_data = oauth->fetchurl(
  	url          	= '/v1/activities/-ACTIVITY-ID-/items'
  	method       	= 'POST'
  	accept_xml   	= 'X'
  	content_type_xml = 'X'
  	cdata        	= cdata
  	).
  CATCH zcx_oauth_error INTO oauth_error.
	error_text = oauth_error->get_text( ).
	MESSAGE error_text TYPE 'E'.
ENDTRY.

Explanation

zcl_oauth=>get_instance

consumer_name - as entered in setup report Z_OAUTH_SETUP_1_API_KEY

screen_name - as entered in setup report Z_OAUTH_SETUP_2_REGISTER_USER

Clone this wiki locally