【问题标题】:Call Instagram API from localhost JAVA从本地 JAVA 调用 Instagram API
【发布时间】:2014-09-12 15:49:36
【问题描述】:

我在 Stackoverflow 上发现了以下问题:Call Instagram API from local host 但答案大多与 php 相关。我使用了Login with Instagram Using OAuth 2.0 in Java Servlets,但我们都知道它只适用于外部服务器。我研究了让 Windows 上的 localhost 公开可用的方法,但失败了。

ngrok 之类的工具可以安全地将本地 Web 服务器公开到 Internet 并捕获流量,但我无法让它工作,到目前为止它导致 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)

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

【问题讨论】:

  • 如果您不同意这个问题,请发表评论.. 因为我是来学习的。也许是正确的工具:tinyserver.sourceforge.net

标签: java localhost instagram


【解决方案1】:

我找到了答案,我想我应该与其他人分享。 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);
  }
  }

【讨论】:

  • 嗨,我面临同样的错误。你能详细说明一下你是怎么做到的吗?
  • 刚刚添加了一个示例。希望对您有所帮助。
  • 请不要忘记给答案评分;)
  • @user48903 你还有这个代码吗?你如何使用那个类?
  • link 这个例子应该足够了,但是你要确保应用我在答案中提到的修复。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
相关资源
最近更新 更多