代码之家  ›  专栏  ›  技术社区  ›  user48903

从本地主机JAVA调用Instagram API

  •  -1
  • user48903  · 技术社区  · 10 年前

    我在Stacksoverflow上发现了以下问题: Call Instagram API from local host 然而,答案大多与php有关。我用过 Login with Instagram Using OAuth 2.0 in Java Servlets 但众所周知,它只在外部服务器上工作。我研究了使Windows上的本地主机公开可用的方法,但失败了。

    有一些工具,如 ngrok 为了安全地将本地web服务器暴露到互联网并捕获流量,我无法让它工作,到目前为止,它的结果是HTTP 500-与我使用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)
    

    也许我使用的工具不好,或者我需要做一项额外的任务。 我很确定,这是可能的,我只是错过了一小块拼图。

    1 回复  |  直到 7 年前
        1
  •  5
  •   Community CDub    7 年前

    我找到了答案,我想我应该和其他人分享。 I should have sent HTTP parameters via POST method instead of GET 。一旦我做出了改变,它就开始工作了。然而,当我在外部托管应用程序时,我找不到一个逻辑解释,为什么Instagram仍然允许通过GET方法发送HTTP参数。

    编辑 -添加示例:

    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);
      }
      }