+ Start a Discussion

REST API sample application



I have successfully completed most of the steps in the REST API tutorial page given in:




I got the link that says "Click here to retrieve contacts from salesforce via REST/Oauth" but when I click on it, the page gives me 404 error.


What could be the issue?




Double check all your URLs.  404 is Not Found and should be easily resolved.



/* OAuthServlet.java */

import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; /** * Servlet parameters */ @WebServlet(name = "oauth", urlPatterns = { "/oauth/*", "/oauth" }, initParams = { // clientId is 'Consumer Key' in the Remote Access UI @WebInitParam(name = "clientId", value = "3MVG9y6x0357HlefZvgrKO4VdcBbtPzTKTKIesHzBn.ZFeKXw5AUd0EN2sdcbGgWgDdg3BA_1QVnij8_Ik3P8"), // clientSecret is 'Consumer Secret' in the Remote Access UI @WebInitParam(name = "clientSecret", value = "4887378198083492253"), // This must be identical to 'Callback URL' in the Remote Access UI @WebInitParam(name = "redirectUri", value = ""), @WebInitParam(name = "environment", value = "https://login.salesforce.com") }) public class OAuthServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String ACCESS_TOKEN = "ACCESS_TOKEN"; private static final String INSTANCE_URL = "INSTANCE_URL"; private String clientId = null; private String clientSecret = null; private String redirectUri = null; private String environment = null; private String authUrl = null; private String tokenUrl = null; @Override public void init() throws ServletException { clientId = this.getInitParameter("clientId"); clientSecret = this.getInitParameter("clientSecret"); redirectUri = this.getInitParameter("redirectUri"); environment = this.getInitParameter("environment"); try { authUrl = environment + "/services/oauth2/authorize?response_type=code&client_id=" + clientId + "&redirect_uri=" + URLEncoder.encode(redirectUri, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new ServletException(e); } tokenUrl = environment + "/services/oauth2/token"; } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String accessToken = (String) request.getSession().getAttribute( ACCESS_TOKEN); if (accessToken == null) { String instanceUrl = null; if (request.getRequestURI().endsWith("oauth")) { // we need to send the user to authorize response.sendRedirect(authUrl); return; } else { System.out.println("Auth successful - got callback"); String code = request.getParameter("code"); HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod(tokenUrl); post.addParameter("code", code); post.addParameter("grant_type", "authorization_code"); post.addParameter("client_id", clientId); post.addParameter("client_secret", clientSecret); post.addParameter("redirect_uri", redirectUri); try { httpclient.executeMethod(post); try { JSONObject authResponse = new JSONObject( new JSONTokener(new InputStreamReader( post.getResponseBodyAsStream()))); System.out.println("Auth response: " + authResponse.toString(2)); accessToken = authResponse.getString("access_token"); instanceUrl = authResponse.getString("instance_url"); System.out.println("Got access token: " + accessToken); } catch (JSONException e) { throw new ServletException(e); } } finally { post.releaseConnection(); } } // Set a session attribute so that other servlets can get the access // token request.getSession().setAttribute(ACCESS_TOKEN, accessToken); // We also get the instance URL from the OAuth response, so set it // in the session too request.getSession().setAttribute(INSTANCE_URL, instanceUrl); } response.sendRedirect(request.getContextPath() + "/DemoREST"); } }


Above is the code I have. I guess you are telling about the authurl that is being set here. But this is the same as given in sample application. I do not exactly know what is the issue.



which URL are you getting the 404 from?  Is it possible that you are having a firewall or proxy issue inhibiting the callback?



It is:




RESTAPITEST is my context path that is in the webapps folder of the tomcat server


I tried turning off the firewall. Can you suggest some other way.?


To provide you more details:


I am developing project in NetBeans. I build the project in Netbeans and then get a war file called RESTAPITES.war.


Then, I am putting this war file in the webapps folder of the remote tomcat server where it gets compiled. Now when I go to :




I can view the link but on clicking the link i get the 404 error.


May be a matter of troubleshooting your deployment to tomcat.  Is the app listening on 8448 ( I know you could have changed the recommended 8443 from the demo).  Code looks good, double check settings on app server. 


I would take the approach of troubleshooting each point of communication and make sure it is accessible and responsive.   You can debug, view the variables returned from SF login and eliminate or identify any issues there.


I am still getting /RESTAPITEST/oauth not available. I have Tomcat 7.0 on my Netbeans which I use to export my project to a war file. I put this file into the webapps folder of the remote Apache Tomcat 6.0 server. Can this Tomcat version change be a problem?