Java HTTP获取对facebook的请求。错误的请求

时间:2014-07-11 14:53:41

标签: facebook-graph-api java-ee tomcat servlets bad-request

我使用浏览器控件编写了一些代码,以便从客户端与Facebook进行交互。

我正在尝试运行servlet应用程序以进行身份​​验证。 servlet重用了我在客户端中使用的类。

url_conn的值是: https://graph.facebook.com/oauth/access_token?client_id= {my-id string}& client_secret = {alphanumeric string secret}& fb_exchange_token = {Really long alphanumeric string}& grant_type = fb_exchange_token 。我删除了使用{}的实际值。

这是代码。怎么了?请提供一些指导。感谢

 try{
        System.out.println("Attempting to open connection");
    conn = (HttpURLConnection) (url_conn.openConnection());
      if (conn.getResponseCode() != 200) {
            System.out.println("Throwing IO Exception Successful on " + url_conn.toString());
        throw new IOException(conn.getResponseMessage());
      }
    //  conn.setRequestProperty("Accept-Charset","UTF-8");
      System.out.println("Attempt Successful");

      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
      String json = reader.readLine();
      JsonReader MyJsonReader = Json.createReader(reader);
      JsonObject jsonObject = MyJsonReader.readObject();

      MyJsonReader.close();
      reader.close();        

      sb = sb.append(jsonObject.toString());    
     }
catch(Exception e)
{
    System.out.println("Error From The FileCounter Class " + e.getMessage());
    e.printStackTrace();

}

  conn.disconnect();      
    }  

1 个答案:

答案 0 :(得分:2)

Facebook只能通过https访问(s表示安全,基本上只是http over SSL / TLS)。尝试使用java的HttpsURLConnection类而不是HttpURLConnection类(http://docs.oracle.com/javase/7/docs/api/javax/net/ssl/HttpsURLConnection.html)。例如:HttpsURLConnection con = (HttpsURLConnection)url.openConnection();