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

Developing Prepopulate Adapter with OIM 11g R2

1.      Prepopulate Adapter in OIM uses the plugin point oracle.iam.request.plugins.PrePopulationAdapte r. 2.      Write the Java code which returns the value which has to be populated on the form. 3.      This code will implement the plugin point oracle.iam.request.plugins.PrePopulationAdapte r. Code Snippet: - package com.oracle.oim.utility.eventhandler; import java.io.Serializable; import java.util.Iterator; import java.util.List; import java.util.logging.Logger; import oracle.iam.identity.exception.NoSuchUserException; import oracle.iam.identity.exception.UserLookupException; import oracle.iam.identity.usermgmt.api.UserManager; import oracle.iam.identity.usermgmt.vo.User; import oracle.iam.platform.Platform; import oracle.iam.platform.authz.exception.AccessDeniedException; import oracle.iam.request.exception.RequestServiceException; import oracle.iam.request.vo.Beneficiary; ...

Oracle Traffic Director (OTD) configuration

Download the OTD software and install it on a server by running runInstaller command from <Binaries>/Disk1. Preferred is to configure the OTD as root user because when the administration server is configured as root, then Oracle Traffic Director starts the keepalived daemon automatically when you start instances that are part of a failover group, and stops the daemon when you stop the instances. Set Oracle_Home as the new Installed OTD Home. Run below command to configure the Admin server: <OTD_HOME>/otd/bin/tadm configure-server --port=8989 --user=admin --server-user=root --instance- home= <OTD_HOME> /otd/instance_name/otd_instance1 This command will ask for admin password and will create the admin server. Run Below command to start the admin server: <OTD_HOME> /otd/instance_name/otd_instance1/admin-server/bin/startserv Login to the OTD console on http://<host>:8989 as admin user.  Click New configuration: Click Next and create ne...

Custom Login Page Protection- OAM 11g R2

Create a login page with fields having username,password and requestid. Below is the sample login page : <%@page language="java" session="true" contentType="text/html;charset=ISO-8859-1"  %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. getServerPort()+path+"/"; String requestID = request.getParameter("request_id"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <font color="blue">Login Page </font><br><br> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Implementing css and javascript</title> <meta http-equi...