-1

I found the following question on Stackoverflow: Call Instagram API from local host however the answers are mostly related to php. I used Login with Instagram Using OAuth 2.0 in Java Servlets but as we all know it only works on external server. I looked into methods to make my localhost on Windows publicly available, but it failed.

There tools like ngrok to securely expose a local web server to the internet and capture traffic, I couldn't get it to work and so far it results HTTP 500 - exact result as I was using localhost:8080.

javax.servlet.ServletException: java.lang.NullPointerException
com.instalogin.CallbackServlet.doGet(CallbackServlet.java:130)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.NullPointerException
com.instalogin.CallbackServlet.getWebContentFromURL(CallbackServlet.java:65)
com.instalogin.CallbackServlet.doGet(CallbackServlet.java:103)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Perhaps the tool that I have used is not good, or I need to do an additional task. I'm pretty sure that, it is possible, and I'm just missing a small piece of a puzzle.

Community
  • 1
  • 1
user48903
  • 109
  • 1
  • 8
  • if you disagree with the question, please leave a comment.. since I'm here to learn. perhaps is the right tool: http://tinyserver.sourceforge.net/ – user48903 Sep 12 '14 at 16:00

1 Answers1

5

I found the answer and I thought I should share it with other folks. I should have sent HTTP parameters via POST method instead of GET. Once I made the changes, it starts working just fine. However, I couldn't find a logical explanation on why Instagram still allows sending HTTP paramters via GET method when I'm hosting the application externally.

EDIT - Adding an example:

public class CallbackServlet 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");


    JSONObject profile = getTokenContent(clientID, clientSecret, redirectURI, code); 
 }

 public JSONObject getTokenContent(String clientID, String clientSecret, String redirectURI, String code){
    try {

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

        URL url = new URL(httpurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");

        String urlParameters = "client_id=" + clientID
                + "&client_secret=" + clientSecret
                + "&grant_type=authorization_code"
                + "&redirect_uri=" + redirectURI
                + "&code="+code;

        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
        conn.setRequestProperty("charset", "utf-8");
        conn.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));

        conn.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream ());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));

        return getJSONFromBufferRd(in);
  }
  }
Community
  • 1
  • 1
user48903
  • 109
  • 1
  • 8