我正在尝试通过 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";
关于java - 为什么我收到 400 响应代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19597810/