Oracle by Example brandingProducing and Consuming Messages with Oracle Identity Cloud Service Authentication

section 0Before You Begin

This 15-minute tutorial shows you how to produce messages and consume messages using REST APIs to an Oracle Event Hub Cloud Service instance that uses Oracle Identity Cloud Service (IDCS) based authentication mechanism.

Background

Oracle Event Hub Cloud Service is a managed, cloud-based offering that provides a highly available and scalable messaging platform for working with streaming data.

An instance of Oracle Event Hub Cloud Service is called a Topic. You can send messages to a Topic and you can read messages from a Topic. A system that sends messages to a Topic is called Producer. A system that reads messages from a Topic is called Consumer.

What Do You Need?

  • A subscription to Oracle Cloud Service.
  • An Oracle Event Hub Cloud Service instance.
  • IDCS username and password.
  • cURL installed in your system. To install cURL, see http://curl.haxx.se/.

section 1Get Topic name and URL

  1. Sign in to Oracle Cloud at https://cloud.oracle.com/ and go the Oracle Event Hub Cloud Service Instances page.
  2. Click on the name of the Topic. A More Information popup with the Topic details will appear.
    moreinfo_popup
    Description of the illustration [moreinfo_popup.jpg]
  3. Copy and save the value in the Topic field. This is the name of the topic to which you will produce a message and later from which you will consume that message.
    moreinfo_topic
    Description of the illustration [moreinfo_topic.jpg]
  4. Copy and save the value in the Topic URL field. The value till restproxy denotes the basepath for the Produce and Consume REST API.
    moreinfo_topicurl
    Description of the illustration [moreinfo_topicurl.jpg]

section 2Get details from Oracle Identity Cloud Service

Perform the following steps to get details from Oracle Identity Cloud Service (IDCS).

  1. In the More Information popup, click the link in the IDCS Application field.
    moreinfo_idcsapp
    Description of the illustration [moreinfo_idcsapp.jpg]
  2. You will be redirected to IDCS application page. Provide your IDCS login credentials. The page with App Details will be displayed.
    idcs_appdetails
    Description of the illustration [idcs_appdetails.jpg]
  3. Click the Configuration tab and expand the General Information section.
    idcs_configuration
    Description of the illustration [idcs_configuration.jpg]
  4. Copy and save the value in the Client ID field.
    idcs_clientid
    Description of the illustration [idcs_clientid.jpg]
  5. Click Show Secret. The Client Secret will be displayed as a popup. Copy and save the value of the Client Secret.
    idcs_clientsecret
    Description of the illustration [idcs_clientsecret.jpg]

    Note: The Client Secret is essentially the client password and should NOT be shared.

  6. In the Configuration tab, expand the Resources section.
    idcs_resources
    Description of the illustration [idcs_resources.jpg]
  7. Copy and save the value in the Primary Audience field.
    idcs_primaryaudience
    Description of the illustration [idcs_primaryaudience.jpg]
  8. Copy and save the value in the Scope field.
    idcs_scope
    Description of the illustration [idcs_scope.jpg]
  9. Copy and save the URL of the current IDCS page. The value till /ui/ denotes the idcsurl for getting the IDCS access token.
    idcs_url
    Description of the illustration [idcs_url.jpg]

section 3Get IDCS Access Token

Perform the following steps to create the IDCS access token using REST request.

  1. Update the following code with appropriate values.
    • clientid: The name of the Client ID. Note that you saved this in the previous section.
    • clientsecret: The value of the client secret. Note that you saved this in the previous section.
    • myusername: Your IDCS username.
    • mypassword: Your IDCS password.
    • myscope: The myscope is the concatenation of the primary audience and scope that you saved in the previous section. For example, in the below code, "https://primary-audience-url.com:443" is the primary audience and "/myfulltopicname" is the scope.
      https://primary-audience-url.com:443/myfulltopicname
    • idcsurl: The Oracle Identity Cloud Service URL for the IDCS instance. Note that you saved this in the previous section.
    									
    curl -k -X POST \
    -u "<clientid>:<clientsecret>" \
    -d "grant_type=password&username=<myusername>&password=<mypassword>&scope=<myscope>" \
    "<idcsurl>/oauth2/v1/token"
    
    

    Note: Additional back slash ( \ ) has been provided to escape new line and string double quotes.

  2. Provide the above code in the command prompt and press enter.
    accesstoken_command
    Description of the illustration [accesstoken_command.jpg]
  3. On successful execution, you will see similar output as given below. In the output, copy and save the value of access_token. This is required in subsequent steps.
    accesstoken_output
    Description of the illustration [accesstoken_output.jpg]

    Note: The access_token cannot be recovered again. There is no means to see the already generated access token. However, you can generate a different access token anytime.


section 4Produce message to Topic

Perform the following steps to produce message to a Topic. A JSON formatted message is used here.

  1. Open Command Prompt.
  2. Update the following code with appropriate values.
    • token: The value of the access_token. Note that you saved this in the earlier section.
    • basepath: The name of the basepath. Note that you saved this in the earlier section.
    • mytopicname: The name of the topic to which you will produce the message. Note that you saved this in the earlier section.
    									
    curl -k -X POST \
    -H "Authorization: Bearer <token>" \
    -H "Content-Type:application/vnd.kafka.json.v2+json" \
    -d "{\"records\":[{\"value\":{\"foo\":\"bar\"}}]}" \
    <basepath>/topics/<mytopicname>
    									

    Note: Additional back slash ( \ ) has been provided to escape new line and string double quotes.

  3. Provide the above code in the command prompt and press enter.
    produce_command
    Description of the illustration [produce_command.jpg]
  4. On successful execution, you will see similar output as given below.
    produce_output
    Description of the illustration [produce_output.jpg]

section 5Create Consumer Group

Perform the following steps to create a consumer group.

  1. Update the following code with appropriate values.
    • token: The value of the access_token. Note that you saved this in the previous section.
    • basepath: The name of the basepath. Note that you saved this in the previous section.
    									
    curl -k -X POST \
    -H "Authorization: Bearer <token>" \
    -H "Content-Type:application/vnd.kafka.json.v2+json" \
    -d "{\"format\": \"json\", \"auto.offset.reset\": \"earliest\"}" \
    <basepath>/consumers/oehcs-consumer-group
    									

    Note: Additional back slash ( \ ) has been provided to escape new line and string double quotes.

  2. Provide the above code in the command prompt and press enter.
    consumergroup_command
    Description of the illustration [consumergroup_command.jpg]
  3. On successful execution, you will see similar output as given below. In the output, copy and save the value of base_uri. This is required in subsequent steps.
    consumergroup_output
    Description of the illustration [consumergroup_output.jpg]

    Note: The base_uri cannot be recovered again. There is no means to see the already generated base_uri. Do save this carefully.


section 6Subscribe to the Topic

You need to subscribe to a Topic before consuming. Perform the following steps to subscribe to the Topic.

  1. Update the following code with appropriate values.
    • token: The value of the access_token. Note that you saved this in the previous section.
    • mytopicname: The name of the topic you want to consume from. This is the same topic to which you produced the message earlier.
    • groupbaseuri: The name of the base_uri that was returned during consumer group creation. Note that you saved this in the previous section.
    									
    curl -k -X POST \
    -H "Authorization: Bearer <token>" \
    -H "Content-Type:application/vnd.kafka.json.v2+json" \
    -d "{\"topics\": [\"<mytopicname>\"]}" \
    <groupbaseuri>/subscription
    									

    Note: Additional back slash ( \ ) has been provided to escape new line and string double quotes.

  2. Provide the above code in the command prompt and press enter.
    subscribe_command
    Description of the illustration [subscribe_command.jpg]
  3. On successful execution, you will see blank screen as output.

section 7Consume message from Topic

Perform the following steps to consume from a Topic.

  1. Update the following code with appropriate values.
    • token: The value of the access_token. Note that you saved this in the previous section.
    • groupbaseuri: The name of the base_uri that was returned during consumer group creation. Note that you saved this in the previous section.
    									
    curl -k -X GET \
    -H "Authorization: Bearer <token>" \
    -H "Accept:application/vnd.kafka.json.v2+json" \
    <groupbaseuri>/records
    									

    Note: Additional back slash ( \ ) has been provided to escape new line and string double quotes.

  2. Provide the above code in the command prompt and press enter.
    consume_command
    Description of the illustration [consume_command.jpg]
  3. On successful execution, you will see similar output as given below.
    consume_output
    Description of the illustration [consume_output.jpg]

more informationWant to Learn More?