@OracleIMC Partner Resources & Training: Discover your Modernization options + Reach new potential through Innovation

Using Oracle Compute API Part 1 of 3 - Shell Script for getting API Authentication Cookie

Allen Wangu
Cloud Consultant

You can programmatically provision and manage Oracle Compute Cloud Service instances and the associated storage and networking resources by using a REST (REpresentational State Transfer) application programming interface (API).

Each REST API call maps to an HTTP request: getting an object (GET), adding an object (POST), updating an object (PUT), and deleting an object (DELETE). The HTTP response code indicates whether the request was successful. Each object for which you can perform the GET, POST, PUT, and DELETE requests is identified uniquely by its URI.

To access Oracle Compute Cloud Service by using the REST API you must use the REST endpoint URL that Oracle provided when your administrator subscribed to the service

Before we get started, here are a couple of requirements:

  1. Knowledge of Shell Scripting
  2. Understanding of Rest Services and JSON
  3. Knowledge of Oracle Compute Orchestration

What is Oracle Compute Authentication Cookie?

For all API calls, Oracle compute require authentication. This can be done either via basic authentication where you provide the username and password or providing an Authentication Cookie. For better security, you should use the Authentication Cookie since it's only valid for 30 minutes. Once it expires you will have to request for a new one.

How do you get one?

If you look at the Quick Start Guide that we provide on Oracle Compute API guide, It takes you 3 steps to get your Authentication Cookie. This script example will help get the cookie in just one command and you can reuse it across different API calls.

1. First you need to the Oracle compute Rest Endpoint. To do this Login to you cloud account > Click on Compute > View Details

oracle compute view details

2. Open an editor of your choice and copy and paste this simple script:

export COMPUTE_COOKIE=$(curl -i -X POST -H "Content-Type: application/oracle-compute-v3+json" -d '{"user":"/Compute-gse00003375/{username}","password":"{xxxxxxxx}"}' https://api-z17.compute.em2.oraclecloud.com/authenticate/ | tr -d '\r' | sed -En 's/^Set-Cookie: (.*)/\1/p' )

3. Replace "{username}" with your cloud account username, replace "{xxxxxxxx}" with your cloud account password.

Take notice that i have my Rest End point in place  


(Replace this with the URL you copied earlier)

In this script I have used the curl command and the sed command to extract the cookie and set it as and environment variable. Please note the echo command is just to verify that the path variable has been set.

Now you can execute the shell script and your Authentication Cookie will be valid for the next 30 minutes. 

NB: If you close you terminal window, you will have to execute the script again to set the Environment variable again.




Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.