//Axis2 client needs a configuration context
ConfigurationContext configContext = null;
String path = null;
GDataRequest request = null;
path = System.getProperty("user.dir");
try {
//Create a configuration context. A configuration context contains information for a
//axis2 environment. This is needed to create an axis2 client
configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(
null, null);
/**
* Call to https://localhost:9443/services/AuthenticationAdmin?wsdl uses HTTPS protocol.
* Therefore we to validate the server certificate. The server certificate is looked up in the
* trust store. Following code sets what trust-store to look for and its JKs password.
* Note : The trust store should have server's certificate.
*/
System.setProperty("javax.net.ssl.trustStore", path + File.separator +"wso2carbon.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
/**
* Here we are authenticating the given user name and password with IS.
* https://localhost:9443/services/AuthenticationAdmin?wsdl
*/
client = new AuthenticationServiceClient(IDENTITY_SERVER + "services/", configContext);
/**
* Actual authentication call. If authentication is successful,
* User can register a consumer secret with IS.
*/
if (client.authenticate(USER_NAME, PASSWORD, IDENTITY_SERVER_HOST_NAME)) {
client.registerOAuthConsumer(CONSUMER_SECRET);
System.out.println("User - " + USER_NAME + " successfully authenticated.");
} else {
System.out.println("Invalid credentials");
return;
}
// We are using Google oauth API to call the service
GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
// Setting user name as consumer key
oauthParameters.setOAuthConsumerKey(USER_NAME);
// Setting above assigned consumer secret
oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
// We will be using HMAC-SHA1 signature. Google API has a class to do that
OAuthHmacSha1Signer signer = new OAuthHmacSha1Signer();
// Create a Google service. The name of the current application given here
// Names are only for reference purpose
GoogleService service = new GoogleService("oauthclient", "sampleapp");
service.setOAuthCredentials(oauthParameters, signer);
/**
* We will be calling test service's echoString method. As parameter we are sending "Hello World"
* The parameter name is "in".
*/
String param = "HelloWorld";
String baseString = ESB_SERVER + "services/OAuthProxy/echoString" + "?xoauth_requestor_id="
+ USER_NAME + "&in=" + param;
/**
* Invoking the request. And writing the response output.
*/
URL feedUrl = new URL(baseString);
request = service.createFeedRequest(feedUrl);
request.execute();
System.out.println(convertStreamToString(request.getResponseStream()));
} catch (AxisFault e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();