为什么我得到400个响应代码? [英] Why am I getting a 400 response code?

查看:164
本文介绍了为什么我得到400个响应代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java(restfb)来使用facebook API。我正在尝试以下代码。

I am trying to get some acquaintance in using facebook API using Java (restfb). I am trying out the following code.

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,这意味着它是一个不好的请求。我想知道为什么当我尝试通过程序运行它时,它的响应不同。我知道我在做错事,但是想知道错误是什么,为什么会发生这样的错误?

When I run the firstReq string in the browser manually, it is redirecting me to the correct page. But when I check the response code I am getting a 400 which means that its a bad request. I want to know why does it respond differently when I try to run it through the program. I know I am doing something wrong, but want to know what is the mistake and why is it occurring? Any kind of insight in this matter would be appreciated.

推荐答案

firstReq url。它包含& scope之间的空格字符。尝试这个(我刚刚删除了空格):

There is an error in the firstReq url. It contains a whitespace character between "& scope". Try this (I just removed the whitespace):

  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";

这篇关于为什么我得到400个响应代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆