繁体   English   中英

为什么我得到400响应码?

[英]Why am I getting a 400 response code?

我试图在使用Java(restfb)使用Facebook API方面结识一些。 我正在尝试以下代码。

public class FBJava {

    private String API_Key = "xxxxxx";
    private String API_Secret = "xxxxxxxx";

    public String firstReq = "https://graph.facebook.com/oauth/authorize?client_id="+API_Key+"&" +
            "redirect_uri=http://www.facebook.com/connect/login_success.html& scope=publish_stream,offline_access,create_event";
    public String secondReq = "https://graph.facebook.com/oauth/access_token?client_id="+API_Key+"" +
            "&redirect_uri=http://www.facebook.com/connect/login_success.html&client_secret="+API_Secret+"&code=";


    public static void main(String[] args) throws IOException {
        FBJava fb = new FBJava();

        System.out.println(fb.firstReq);
        URL request = new URL(fb.firstReq);

        HttpURLConnection conn = (HttpURLConnection) request.openConnection();
        conn.connect();

        int code = conn.getResponseCode();
        System.out.println(code);

    }
}

当我在浏览器中手动运行firstReq字符串时,它会将我重定向到正确的页面。 但是,当我检查响应代码时,我得到了400,这意味着它是一个错误的请求。 我想知道为什么当我尝试通过程序运行它时会有不同的响应。 我知道我做错了什么,但是想知道什么是错误以及为什么会发生? 在这方面的任何见识将不胜感激。

firstReq网址中有错误。 它在“&范围”之间包含一个空格字符。 试试这个(我刚刚删除了空格):

  public String firstReq = "https://graph.facebook.com/oauth/authorize?client_id="+API_Key+"&" +
    "redirect_uri=http://www.facebook.com/connect/login_success.html&scope=publish_stream,offline_access,create_event";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM