【问题标题】:Java HTTP get request to facebook. Bad requestJava HTTP 获取对 facebook 的请求。错误的请求
【发布时间】:2014-07-11 22:58:52
【问题描述】:

我编写了一些代码,使用有效的浏览器控件从客户端与 facebook 交互。

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

url_conn 的值为: https://graph.facebook.com/oauth/access_token?client_id={my-id string}&client_secret={字母数字字符串 secret}&fb_exchange_token={真长的字母数字字符串}&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();      
    }  

【问题讨论】:

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


    【解决方案1】:

    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();

    【讨论】:

    • 非常感谢。此外,我不小心剪断了我的钥匙。它现在固定
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    相关资源
    最近更新 更多