use*_*903 -1 java localhost instagram
我在 Stackoverflow 上发现了以下问题:Call Instagram API from local host但是答案主要与 php 有关。我在 Java Servlet 中使用 OAuth 2.0 使用 Instagram 登录,但众所周知,它仅适用于外部服务器。我研究了使 Windows 上的本地主机公开可用的方法,但失败了。
有像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)
Run Code Online (Sandbox Code Playgroud)
也许我用过的工具不好,或者我需要做一个额外的任务。我很确定,这是可能的,而且我只是遗漏了一小块拼图。
我找到了答案,我想我应该与其他人分享。 我应该通过 POST 方法而不是 GET 发送 HTTP 参数。一旦我进行了更改,它就会开始正常工作。但是,当我在外部托管应用程序时,我找不到关于为什么 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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6640 次 |
| 最近记录: |