function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
subaasubaa 

How to call our custom Servlet from Salesforce login page using API?

Hi,

 

I used the AuthServlet sample code in my org to test REST API functionality. I able to reach upto Salesforce login page. But when I gave my credential, it throws the following error.

 

java.lang.IllegalStateException

at org.apache.catalina.connector.ResponseFacade.sendRedirect(

ResponseFacade.java:435)....

 

doGet method in AuthServlet.java sample code (http://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_REST_API), that I used is,

 

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("grant_type", "password");
    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) {
      e.printStackTrace();
      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");
 }

 

I read some where like, response is already commited for the request and hence it throws the error. Can anyone help me, how to call our servlet once the credentials provided in the Salesforce login page.

Best Answer chosen by Admin (Salesforce Developers) 
subaasubaa

Hi,

 

I got this issue, because of that I didn't mentioned, '/oauth/_callback' URLPattern for the AuthServlet file. I changed my web.xml file and ran my application. Now I able to control over the servlet and get back the access token that was retrieved in the first request.

 

Regards,

SuBaa

All Answers

subaasubaa

Hi,

 

On top of to my previous query, let me add this one.

 

As per the document, I defined the callback url as '

http://localhost:8080/MyWebAppln/oauth/_callback', but i am getting HTTP 404 error like, 'the requested resource (/MyWebAppln/oauth/_callback) is not available' when I submit my Salesforce credential in the login page.

 

Hope you understand my ploblem now and I am expecting your response.

 

Regards,

SuBaa

subaasubaa

Hi,

 

I got this issue, because of that I didn't mentioned, '/oauth/_callback' URLPattern for the AuthServlet file. I changed my web.xml file and ran my application. Now I able to control over the servlet and get back the access token that was retrieved in the first request.

 

Regards,

SuBaa

This was selected as the best answer