MSauer
MSauer

Reputation: 148

Facebook Graph API only returns 'success true' JSON

I would to create a facebook client as android app. I have already implementet an successful login process. Now i want to get information (simple request, only default values so far) of my profile.

I am opening the graph url

https://graph.facebook.com/me?access_token={ACCESS_TOKEN}

with the Android App (The webclient should work, because i use exactly the same on an other app) the Response is:

{"success":true}

The Response headers are:

null: HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, must-revalidate
Connection: keep-alive
Content-Length: 16
Content-Type: text/javascript; charset=UTF-8
Date: Mon, 27 Mar 2017 19:53:18 GMT
Expires: Sat, 01 Jan 2000 00:00:00 GMT
facebook-api-version: v2.8
Pragma: no-cache
X-Android-Received-Millis: 1490644395774
X-Android-Response-Source: NETWORK 200
X-Android-Selected-Protocol: http/1.1
X-Android-Sent-Millis: 1490644395542
X-FB-Debug: {X-FB-DEBUG}
x-fb-rev: 2916655
x-fb-trace-id: {x-fb-trace-id}

When i open the exactly same url on my Browser (Chrome & Firefox) i get the expected response:

{
   "name": "First_name Surname",
   "id": "123456789"
}

It seems like facebook doesnt want to redirect me to another page, so the response should be the same.

The code of the webclient in the app is like following:

class postRequest_runtimeTask implements Runnable
        {
            private String url;
            private HashMap<String, String> data;
            private postRequest_runtimeTask(String urlS, HashMap<String, String> dataS) {url = urlS; data = dataS;};

            @Override
            public void run()
            {
                URL requestUrl;

                try
                {
                    requestUrl = new URL(url);

                    HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
                    conn.setReadTimeout(15000);
                    conn.setConnectTimeout(15000);
                    conn.setRequestMethod("POST");
                    conn.setDoInput(true);
                    conn.setDoOutput(true);

                    /*

                    for(String key : conn.getHeaderFields().keySet())
                    {
                        List<String> tmp = conn.getHeaderFields().get(key);

                        for(String tmpval : tmp)
                        {
                            Log.v("Header " + key, tmpval);
                        }
                    }
                    */

                    /* Build String from HashMap for POST-Params */
                    StringBuilder requestDataTmp = new StringBuilder();

                    boolean first = true;

                    for(Map.Entry<String, String> entry : data.entrySet())
                    {
                        if(first)
                        {
                            first = false;
                        }
                        else
                        {
                            requestDataTmp.append("&");
                        }

                        requestDataTmp.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                        requestDataTmp.append("=");
                        requestDataTmp.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
                    }

                    String requestData = requestDataTmp.toString();

                    OutputStream os = conn.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                    writer.write(requestData);

                    writer.flush();
                    writer.close();
                    os.close();
                    int responseCode=conn.getResponseCode();

                    TEMP_BUFFER = "";

                    if (responseCode == HttpsURLConnection.HTTP_OK)
                    {
                        String line;
                        BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));

                        while((line=br.readLine()) != null)
                        {
                            TEMP_BUFFER += line;
                        }
                    }
                    else
                    {
                        //TEMP_BUFFER = "";

                        Log.e("HTTP RESPONSE CODE", String.valueOf(responseCode) + ", url: " + url + ", content: \n\n" + TEMP_BUFFER);
                    }
                }
                catch(Exception exp)
                {
                    exp.printStackTrace();
                }
            }
        }

        Thread postRequest_runtimeTastAsync = new Thread(new postRequest_runtimeTask(url, data));
        postRequest_runtimeTastAsync.start();

Thank you!

Upvotes: 0

Views: 626

Answers (1)

fazega
fazega

Reputation: 317

You are using a POST request. The api answer only the "success" field on a POST request (https://developers.facebook.com/docs/pages/managing).

You must use a GET request instead.

Upvotes: 1

Related Questions