服务器返回HTTP响应代码:400为URL:

时间:2016-08-03 14:30:36

标签: java json

我收到此错误:
java.io.IOException:服务器返回HTTP响应代码:400为URL:https://graph.facebook.com/search?q=java&type=post     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)     在java.net.URL.openStream(未知来源)     在Main.main(Main.java:19)

代码:

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import org.json.JSONObject;
import javax.json.*;



public class Main {
    public static void main(String[] args) {
          try {
              URL url = new URL("https://graph.facebook.com/search?q=java&type=post");
              InputStream is = url.openStream();
              JsonReader rdr = Json.createReader(is);        
              JsonObject obj = rdr.readObject();
              JsonArray results = obj.getJsonArray("data");
              for (JsonObject result : results.getValuesAs(JsonObject.class)) {
                  System.out.print(result.getJsonObject("from").getString("name"));
                  System.out.print(": ");
                  System.out.println(result.getString("message", ""));
                  System.out.println("-----------");
              }
          } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我不明白为什么?

1 个答案:

答案 0 :(得分:0)

当在浏览器中尝试该URL时,我得到回复:

{
   "error": {
      "message": "An access token is required to request this resource.",
      "type": "OAuthException",
      "code": 104,
      "fbtrace_id": "Du6VLoot53K"
   }
}

它的响应代码也是400,所以看起来很明显为什么你得到它:
需要访问令牌。

我的代码中没有看到任何尝试提供此类访问令牌的内容。