1

I have a simple servlet named Autentica that is responsible to get de code parameter used to know the OAuth token. The API returns to my callback page and thus the servlet named callback is runned. The problem is here: when I build the URL to get the OAuth token is generated an IOException that I dont know how to solve.

Anyone can help-me? Follows the servlet codes and the console results. Tks a lot!

1) Servlet Autentica

@WebServlet(name = "autentica", urlPatterns = {"/autentica"})
public class Autentica extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        processRequest(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        processRequest(req, resp);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        //API Credentials
        String client_id = "XXXXXXXXXXX";
        String client_secret = "YYYYYYYYYYYY";
        String redirect_uri = "http://localhost:8080/InstagramAPI/callback";

        //Set Session Variables
        HttpSession session = request.getSession(true);
        session.setAttribute("client_id", client_id);
        session.setAttribute("client_secret", client_secret);
        session.setAttribute("redirect_uri", redirect_uri);

        try {
            //Redirect User to foursquare login page
            String url = "https://api.instagram.com/oauth/authorize/?client_id=" 
                    + client_id + "&redirect_uri=" + redirect_uri
                    + "&response_type=code";
            response.sendRedirect(url);
        } finally {
            out.close();
        }
    }
}

2) Servlet Callback

@WebServlet(name = "callback", urlPatterns = {"/callback"})
public class Callback extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        processRequest(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        processRequest(req, resp);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(true);
        String clientID =(String)session.getAttribute("client_id");
        String clientSecret =(String)session.getAttribute("client_secret");
        String redirectURI =(String)session.getAttribute("redirect_uri");  
        String code = request.getParameter("code");

        String url = "https://api.instagram.com/oauth/access_token?"
                + "client_id=" + clientID
                + "&client_secret=" + clientSecret
                + "&grant_type=authorization_code"
                + "&redirect_uri=" + redirectURI
                + "&code="+code;
        getContent(url);
    }

    //Return response after GET Request
    public String getContent(String httpurl){
        try {
            URL url = new URL(httpurl);
            URLConnection httpc = url.openConnection();
            httpc.setDoInput(true);
            httpc.connect();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    httpc.getInputStream()));
            String strLine = "";
            String content = "";
            while ((strLine = in.readLine()) != null){
                 content = content+strLine;
            }
            in.close();
            return content;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

3) Console

SEVERE: java.io.IOException: Server returned HTTP response code: 405 for URL: https://api.instagram.com/oauth/access_token?client_id=XXXXXXXXXXX&client_secret=YYYYYYYYYYYY&grant_type=authorization_code&redirect_uri=http://localhost:8080/InstagramAPI/callback&code=aca1134c71944461b9db9417f4e2baa5
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at Callback.getContent(Callback.java:62)
    at Callback.processRequest(Callback.java:52)
    at Callback.doGet(Callback.java:29)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

SEVERE:     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
SEVERE:     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
SEVERE:     at Callback.getContent(Callback.java:62)
SEVERE:     at Callback.processRequest(Callback.java:52)
SEVERE:     at Callback.doGet(Callback.java:29)
SEVERE:     at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
SEVERE:     at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
SEVERE:     at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
SEVERE:     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
SEVERE:     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
SEVERE:     at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
SEVERE:     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
SEVERE:     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
SEVERE:     at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
SEVERE:     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
SEVERE:     at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
SEVERE:     at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
SEVERE:     at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
SEVERE:     at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
SEVERE:     at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
SEVERE:     at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
SEVERE:     at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
SEVERE:     at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
SEVERE:     at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
SEVERE:     at java.lang.Thread.run(Thread.java:722)
reuben
  • 3,360
  • 23
  • 28
Bruno Moreno
  • 35
  • 2
  • 8

1 Answers1

1

As your getting an Error 405 that means:

405 Method Not Allowed

A request was made of a resource using a request method not supported by that resource; for example, using GET on a form which requires data to be presented via POST, or using PUT on a read-only resource.

You are sending a GET request, but Instagram is insisting on a POST:

In order to make this exchange, you simply have to POST this code, along with some app identification parameters to our access_token endpoint.

To use URLConnection with Post, check this answer.

Community
  • 1
  • 1
Jan Gerlinger
  • 7,361
  • 1
  • 44
  • 52
  • Hi @jan-gerlinger, I'm not an expetise in web programming and maybe I'm doing some idiot question. Sorry about it... In really I do what you suggest and now I'm getting the error 400 (bad request I think). Follows part of my method getContent. `URL url = new URL(httpurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream()));` – Bruno Moreno Jul 05 '12 at 17:39
  • Another question... if I just copy and past the URL of httpurl variable at a browser I should see the content? Because there is nothing in my browser. – Bruno Moreno Jul 05 '12 at 17:42
  • When your typing a URL in your browser like https://api.instagram.com/oauth/access_token?client_id=test, your browser automatically sends a `GET` request and your getting the same `Error 405` (you can check that e.g. in the web console from Firefox). For the new `Error 400` please ask a new question with your updated code. – Jan Gerlinger Jul 06 '12 at 13:10
  • OK, I will do it.Thanks a lot. – Bruno Moreno Jul 06 '12 at 19:08