Today I am going to show you how to consume an oAuth 1.0 secured rest service using a java spring rest template class.
We can create a simple standalone class to do this exercise. so let's start by creating simple java spring application.
1. Let's add a new class to the project as MyOAuthSample.java
2. Create a main method so we can execute the application independently. In the main method we will create an instance of MyOAuthSample class and call the showCourseMode() method.
3. Write showCourseMode() method. here we will be using spring provided OAuthRestTemplate class to call the rest service. This template requires OAuth parameters to create. they are passed via getResouces().
4. Create getResouces() method as following. Here we will be using BaseProtectedResourceDetails which is an implementation of ProtectedResourceDetails interface. Set consumer key and shared the secret as below.
5. Now let's add a response type object. Here I am creating it as CourseDetailsRestResponse. this class will be used to map the response directly. Here I have added only a single parameter so the response will be only to a single value and other values are skipped.
6. Now I will create another method to create the rest URL.
7. Now every thing is ready so we can call the rest service. so add below lines to the end of the method showCourseMode().
Following is the full source code of the project.
MyOAuthSample.java
CourseDetailsRestRespons.
We can create a simple standalone class to do this exercise. so let's start by creating simple java spring application.
1. Let's add a new class to the project as MyOAuthSample.java
2. Create a main method so we can execute the application independently. In the main method we will create an instance of MyOAuthSample class and call the showCourseMode() method.
public static void main(String[] args) {
new MyOAuthSample().showCourseMode();
}
new MyOAuthSample().showCourseMode();
}
3. Write showCourseMode() method. here we will be using spring provided OAuthRestTemplate class to call the rest service. This template requires OAuth parameters to create. they are passed via getResouces().
private void showCourseMode() {
OAuthRestTemplate authRestTemplate=new OAuthRestTemplate(getResouces());
}
OAuthRestTemplate authRestTemplate=new OAuthRestTemplate(getResouces());
}
4. Create getResouces() method as following. Here we will be using BaseProtectedResourceDetails which is an implementation of ProtectedResourceDetails interface. Set consumer key and shared the secret as below.
private ProtectedResourceDetails getResouces() {
BaseProtectedResourceDetails resourceDetails=new BaseProtectedResourceDetails();
resourceDetails.setConsumerKey("yourConsumerKey");
resourceDetails.setSharedSecret(new SharedConsumerSecretImpl("yourSharedSecret"));
return resourceDetails;
}
BaseProtectedResourceDetails resourceDetails=new BaseProtectedResourceDetails();
resourceDetails.setConsumerKey("yourConsumerKey");
resourceDetails.setSharedSecret(new SharedConsumerSecretImpl("yourSharedSecret"));
return resourceDetails;
}
5. Now let's add a response type object. Here I am creating it as CourseDetailsRestResponse. this class will be used to map the response directly. Here I have added only a single parameter so the response will be only to a single value and other values are skipped.
package com.nuwan.rest.callCourseMgmt;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder = { "courseMode" })
@XmlRootElement(name = "course")
public class CourseDetailsRestResponse {
private String courseMode;
@XmlElement
public String getCourseMode() {
return courseMode;
}
public void setCourseMode(String courseMode) {
this.courseMode = courseMode;
}
}
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder = { "courseMode" })
@XmlRootElement(name = "course")
public class CourseDetailsRestResponse {
private String courseMode;
@XmlElement
public String getCourseMode() {
return courseMode;
}
public void setCourseMode(String courseMode) {
this.courseMode = courseMode;
}
}
6. Now I will create another method to create the rest URL.
private URI getUri() {
URI uri=null;
try {
uri=new URI("http://yoururl.com/rest/course/nafshun46086");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return uri;
}
URI uri=null;
try {
uri=new URI("http://yoururl.com/rest/course/nafshun46086");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return uri;
}
7. Now every thing is ready so we can call the rest service. so add below lines to the end of the method showCourseMode().
CourseDetailsRestResponse response=
authRestTemplate.getForObject(getUri(), CourseDetailsRestResponse.class);
System.out.println(response.getCourseMode());
authRestTemplate.getForObject(getUri(), CourseDetailsRestResponse.class);
System.out.println(response.getCourseMode());
Following is the full source code of the project.
MyOAuthSample.java
package com.nuwan.rest.callCourseMgmt;
import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.security.oauth.common.signature.SharedConsumerSecretImpl;
import org.springframework.security.oauth.consumer.BaseProtectedResourceDetails;
import org.springframework.security.oauth.consumer.ProtectedResourceDetails;
import org.springframework.security.oauth.consumer.client.OAuthRestTemplate;
public class MyOAuthSample{
public static void main(String[] args) {
System.out.println(mycalsss.class.toString());
new MyOAuthSample().showCourseMode();
}
private void showCourseMode() {
OAuthRestTemplate authRestTemplate=new OAuthRestTemplate(getResouces());
CourseDetailsRestResponse response=
authRestTemplate.getForObject(getUri(), CourseDetailsRestResponse.class);
System.out.println(response.getCourseMode());
}
private URI getUri() {
URI uri=null;
try {
uri=new URI("http://yoururl.com/rest/course/nafshun46086");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return uri;
}
private ProtectedResourceDetails getResouces() {
BaseProtectedResourceDetails resourceDetails=new BaseProtectedResourceDetails();
resourceDetails.setConsumerKey("yourConsumerKey");
resourceDetails.setSharedSecret(new SharedConsumerSecretImpl("yourSharedSecret"));
return resourceDetails;
}
}
import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.security.oauth.common.signature.SharedConsumerSecretImpl;
import org.springframework.security.oauth.consumer.BaseProtectedResourceDetails;
import org.springframework.security.oauth.consumer.ProtectedResourceDetails;
import org.springframework.security.oauth.consumer.client.OAuthRestTemplate;
public class MyOAuthSample{
public static void main(String[] args) {
System.out.println(mycalsss.class.toString());
new MyOAuthSample().showCourseMode();
}
private void showCourseMode() {
OAuthRestTemplate authRestTemplate=new OAuthRestTemplate(getResouces());
CourseDetailsRestResponse response=
authRestTemplate.getForObject(getUri(), CourseDetailsRestResponse.class);
System.out.println(response.getCourseMode());
}
private URI getUri() {
URI uri=null;
try {
uri=new URI("http://yoururl.com/rest/course/nafshun46086");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return uri;
}
private ProtectedResourceDetails getResouces() {
BaseProtectedResourceDetails resourceDetails=new BaseProtectedResourceDetails();
resourceDetails.setConsumerKey("yourConsumerKey");
resourceDetails.setSharedSecret(new SharedConsumerSecretImpl("yourSharedSecret"));
return resourceDetails;
}
}
CourseDetailsRestRespons.
package com.nuwan.rest.callCourseMgmt;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder = { "courseMode" })
@XmlRootElement(name = "course")
public class CourseDetailsRestResponse {
private String courseMode;
@XmlElement
public String getCourseMode() {
return courseMode;
}
public void setCourseMode(String courseMode) {
this.courseMode = courseMode;
}
}
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder = { "courseMode" })
@XmlRootElement(name = "course")
public class CourseDetailsRestResponse {
private String courseMode;
@XmlElement
public String getCourseMode() {
return courseMode;
}
public void setCourseMode(String courseMode) {
this.courseMode = courseMode;
}
}
Comments
Post a Comment