从localhost JAVA调用Instagram API
我在Stackoverflow上发现了以下问题:然而,答案大多与php有关。我用过,但我们都知道它只在外部服务器上工作。我研究了使Windows上的本地主机公开可用的方法,但失败了 有一些工具喜欢将本地web服务器安全地公开到internet并捕获流量,但我无法让它工作,到目前为止,它产生了HTTP 500-这与我使用localhost:8080时的结果完全相同从localhost JAVA调用Instagram API,java,localhost,instagram,Java,Localhost,Instagram,我在Stackoverflow上发现了以下问题:然而,答案大多与php有关。我用过,但我们都知道它只在外部服务器上工作。我研究了使Windows上的本地主机公开可用的方法,但失败了 有一些工具喜欢将本地web服务器安全地公开到internet并捕获流量,但我无法让它工作,到目前为止,它产生了HTTP 500-这与我使用localhost:8080时的结果完全相同 javax.servlet.ServletException: java.lang.NullPointerException com.
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)
也许我使用的工具不好,或者我需要做额外的任务。
我很确定,这是可能的,我只是错过了一个小难题。我找到了答案,我想我应该和其他人分享。 . 一旦我做了改变,它就开始正常工作了。然而,我找不到一个合理的解释来解释为什么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你还有这个密码吗?你如何使用那个课程?这个例子应该足够了,但是您需要确保应用我在回答中提到的修复。祝你好运