Take the 2-minute tour ×

I am trying to make a "login with facebook" button server side.

I suceed into getting access_token but when I call the url to receive the json object with user info, the response body content returns simply and only "True"

The worst is, if I paste the url in the same browser window, the browser brings the correct json text in the response body.

Thats is cause I make this call server side and the code that make the call goes here:

String token_url = "https://graph.facebook.com/oauth/access_token?"
    + "client_id=" + app_id + "&redirect_uri=" + URLEncoder.encode(my_url, "UTF-8")
    + "&client_secret=" + app_secret + "&code=" + code;

String response = Util.sendGET(token_url, "GET", "application/json");

And the function sendGET follows:

public static String sendGET (String pageUrl, String method, String contentType) {
    String pagecontent = "";
    try {
        String stuff = null;

        java.net.URL url = new java.net.URL(pageUrl);
        java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
        conn.setRequestProperty("Content-Type", contentType);

        DataOutputStream printout = new java.io.DataOutputStream(conn.getOutputStream ());
        BufferedReader in = new java.io.BufferedReader(
                                        new InputStreamReader(conn.getInputStream())
        while ((stuff = in.readLine()) != null) {
            pagecontent += stuff; 
        System.out.println("Page Content = " + pagecontent);
    } catch (Exception e) {
    return pagecontent;

As I said the result of pagecontent is "true" string, but if I paste de token_url content on browser it brings the corrent json object.

share|improve this question
I would recommend you use existing FB API library for java restfb.com to save your time... –  Pau Kiat Wee Apr 4 '12 at 5:03
i can't see the url to receive the json object with user info in your code –  Luca Rocchi Apr 4 '12 at 6:32
You may not be passing an appropriate Accept: header in your HTTP request. –  Barend Apr 4 '12 at 7:11
+1 for @PauKiatWee's suggestion. What you've written is very close to the RestFB DefaultWebRequestor.executeGet method, which works for me. RestFB doesn't set Content-Type - perhaps you could try removing that header? –  Richard Barnett Apr 4 '12 at 13:14
add comment


Your Answer



Browse other questions tagged or ask your own question.