Skip to main content

Retrieving OAM OAuth access token through Java

Create a OAuth client in OAM. Encode the username and password of the client in "username:password" format. This can be achieved from https://www.base64encode.org/ site.

Once OAuth client is created it can be checked using below command:

curl -i -H 'Authorization: Basic <Encoded Username:Password>' -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" --request POST http://<OAM_host>:14100/ms_oauth/oauth2/endpoints/oauthservice/tokens -d 'grant_type=password&username=<OAM_Admin>&password=<OAM_Admin_Pwd>'  

If the response is 200 and access token is retrieved then OAuth service is working fine.

Same access token can be retrieved using below sample code :


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import org.json.JSONObject;
import java.net.HttpURLConnection;
import java.net.URL;

public class CheckRest {

public static void main(String[] args) {
URL url;
try {

url = new URL("http://<OAM_Host>:14100/ms_oauth/oauth2/endpoints/oauthservice/tokens");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
httpCon.setRequestProperty("Authorization","Basic b21zc2NsaWVudDpPcmFjbGUxMjM=");
httpCon.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
String urlParameters = "grant_type=password&username=<OAM_Admin>&password=<OAM_Admin_Pwd>";

// Send post request
httpCon.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpCon.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = httpCon.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(
httpCon.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) 
{
response.append(inputLine);
}
in.close();

// print result
System.out.println(response.toString());
JSONObject results = new JSONObject(response.toString());
results.get("access_token");
System.out.println("access token = " + results.get("access_token"));

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Comments

  1. Hi! Do you have some tutorial about how to configure this in OAM?
    Tks!

    ReplyDelete

Post a Comment

Popular posts from this blog

Adding UDF (User Defined Field) on create user page OIM 11g R2 PS1:-

    Login to Sysadmin console and create a Sandbox and activate it. Click on Form Designer and search for user form. 2.      Create a new field of desired type. 3.      Provide the Required Values for UDF creation and click save and close. UDF field will be added then publish the sand box. 4.      Login to Identity console now and create another sandbox and activate it. After sandbox is activated click on users link and the click on create user. 5.      Provide the mandatory values on the form and then click the customize button on top. Select source from the view. 6.      Select the panel where the field has to be added. Select panel form lay out click add content. Select Data component Catalog from the box. 7.      Select UserVO from the bottom. ...

Oracle Identity Cloud Service (IDCS)

   Oracle Identity Cloud Service i s the next generation comprehensive security and identity platform that is cloud-native and designed to be an integral part of the enterprise security fabric, providing modern identity for modern applications.  IDCS is a cloud-based identity management system that works by associating specific rights and restrictions with each user’s established identity. User provisioning, access control, and directory services are critical components of Oracle’s cloud-based security portfolio. Oracle Identity Cloud Service has been designed to meet the needs of organizations in a number of typical use-case scenarios, such as the cloud (allowing both on-premise and cloud resources to be secured from a single set of controls), mobile access (providing sign on for native or browser-based apps), employee-facing intranet and customer-facing extranet solutions.  Key Features: Oracle Identity Cloud Service provides the following functionali...

Setting Java in Linux

After setting Java_home and Path variable also /usr/bin/java points to the open jdk in Linux. To resolve this we need to establish the link between usr/bin/java and the new JDK installed. Run below command as root to move the java: mv /usr/bin/java /home/ After this run below command to establish the link: ln -s /usr/bin/java /<New_JAVA_HOME/bin/java>