INVOKE HTTPS URL AND OAUTH2 GETTING AUTHENTICATION TOKEN FROM WSO2 IDENTITY SERVER pre-requisities:
1) org.apache.oltu.oauth2.common-0.31.jar
2) org.apache.oltu.oauth2.client-0.31.jar
import java.security.SecureRandom; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.oltu.oauth2.client.OAuthClient; import org.apache.oltu.oauth2.client.URLConnectionClient; import org.apache.oltu.oauth2.client.request.OAuthClientRequest; import org.apache.oltu.oauth2.client.response.OAuthClientResponse; import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse; import org.apache.oltu.oauth2.common.exception.OAuthProblemException; import org.apache.oltu.oauth2.common.exception.OAuthSystemException; import org.apache.oltu.oauth2.common.message.types.GrantType; public class TestAuth { protected static void init() { try { SSLContext sc = SSLContext.getInstance("SSL"); HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String urlHostName, SSLSession session) { return true; } }; TrustManager[] trustAllCerts = { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; sc.init(null, trustAllCerts, new SecureRandom()); SSLContext.setDefault(sc); HttpsURLConnection.setDefaultHostnameVerifier(hv); } catch (Exception e) { e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { init(); try { OAuthClientRequest request = OAuthClientRequest .tokenLocation("https://localhost:9447/oauth2/token") .setGrantType(GrantType.PASSWORD) .setClientId("v79zb9om_TRquw1AVwxfsfV2inQa") .setClientSecret("H8S8qfY6GsjNOJKZ9y8juvkb_9Ya") .setUsername("admin").setPassword("admin") .buildBodyMessage(); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); oAuthClient.accessToken(request); OAuthJSONAccessTokenResponse tokenResponse = (OAuthJSONAccessTokenResponse) oAuthClient .accessToken(request, OAuthJSONAccessTokenResponse.class); System.out.println("TOKEN :" + tokenResponse.getAccessToken()); } catch (OAuthSystemException e) { e.printStackTrace(); } catch (OAuthProblemException e) { e.printStackTrace(); } } }