gpt4 book ai didi

java - 使用 Java 访问 Instagram API 时出现 400 错误

转载 作者:行者123 更新时间:2023-11-29 05:57:50 25 4
gpt4 key购买 nike

我有一个名为 Autentica 的简单 servlet,它负责获取用于了解 OAuth token 的代码参数。 API 返回到我的回调页面,因此运行名为 callback 的 servlet。问题是我无法从 OAuth 获取 token ,因为我从服务器收到错误 400。我不是网络编程专家,可能是新手错误。

谁能帮帮我?遵循 servlet 代码和控制台结果。非常感谢!

1) 小服务程序

@WebServlet(name = "autentica", urlPatterns = {"/autentica"})
public class Autentica 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 {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

//API Credentials
String client_id = "XXXX";
String client_secret = "YYYY";
String redirect_uri = "http://localhost:8080/InstagramAPI/callback";

//Set Session Variables
HttpSession session = request.getSession(true);
session.setAttribute("client_id", client_id);
session.setAttribute("client_secret", client_secret);
session.setAttribute("redirect_uri", redirect_uri);

try {
//Redirect User to foursquare login page
String url = "https://api.instagram.com/oauth/authorize/?client_id="
+ client_id + "&redirect_uri=" + redirect_uri
+ "&response_type=code";
response.sendRedirect(url);
} finally {
out.close();
}
}
}

2) Servlet 回调

@WebServlet(name = "callback", urlPatterns = {"/callback"})
public class Callback 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");

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

//Return response after GET Request
public String getContent(String httpurl){
try {
URL url = new URL(httpurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String strLine = "";
String content = "";
while ((strLine = in.readLine()) != null){
content = content+strLine;
}
in.close();
System.out.println("DEBUG = "+content);
return content;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

3) 控制台

INFO: InstagramAPI was successfully deployed in 990 milliseconds.
SEVERE: java.io.IOException: Server returned HTTP response code: 400 for URL: https://api.instagram.com/oauth/access_token?client_id=XXXX&client_secret=YYYY&grant_type=authorization_code&redirect_uri=http://localhost:8080/InstagramAPI/callback&code=7049a73c0dfe43528115fe1a52ff4e0e
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at Callback.getContent(Callback.java:61)
at Callback.processRequest(Callback.java:52)
at Callback.doGet(Callback.java:29)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)

SEVERE: at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
SEVERE: at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
SEVERE: at Callback.getContent(Callback.java:61)
SEVERE: at Callback.processRequest(Callback.java:52)
SEVERE: at Callback.doGet(Callback.java:29)
SEVERE: at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
SEVERE: at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
SEVERE: at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
SEVERE: at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
SEVERE: at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
SEVERE: at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
SEVERE: at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
SEVERE: at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
SEVERE: at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
SEVERE: at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
SEVERE: at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
SEVERE: at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
SEVERE: at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
SEVERE: at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
SEVERE: at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
SEVERE: at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
SEVERE: at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
SEVERE: at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
SEVERE: at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
SEVERE: at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
SEVERE: at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
SEVERE: at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
SEVERE: at java.lang.Thread.run(Thread.java:722)

最佳答案

您将数据错误地发送到 access_token URL。您需要发送 POST 请求数据中的字段。不作为 URL 的一部分。 URL 应仅为“https://api.instagram.com/oauth/access_token”,其余数据应采用 urlencode 形式作为 POST 中的数据。

关于java - 使用 Java 访问 Instagram API 时出现 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11369409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com