Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can do facebook batch fql in java

in facebook fql theres this code

https://developers.facebook.com/docs/reference/api/batch/


curl \
    -F 'access_token=…' \
    -F 'batch=[ \
            {"method": "GET", "relative_url": "me"}, \
            {"method": "GET", "relative_url": "me/friends?limit=50"} \
        ]'\
    https://graph.facebook.com

it suppose to to be sent with json but I really dont understand how to do this any help ?

thanks

like image 459
Peril Avatar asked Oct 08 '11 12:10

Peril


3 Answers

You can simple use the BatchFB api its very powerful and easy , you dont have to deal will all of these stuff and it use the fql for example to get all your friends

Later<ArrayNode> friendsArrayList = this.Batcher.query("SELECT uid FROM user WHERE uid  IN (SELECT uid2 FROM friend WHERE uid1 = me())");
    for (JsonNode friend : friendsArrayList.get()) {
            .......
        }

and its batched

like image 172
salahy Avatar answered Oct 31 '22 16:10

salahy


I believe your question is how to execute a batch request using Facebook Graph API. For this you have to issue a POST request to

"https://graph.facebook.com" 

and the post data to be sent should be

"batch=[{'method': 'GET', 'relative_url': 'me'}, {'method': 'GET', 'relative_url': 'me/friends?limit=50'}]&access_token=@accesstoken" 

in your case [@accesstoken must be replaced with your access token value].

This request will return the details of the owner of the access token(normally the current logged in user) and a list of 50 facebook friends(contains id and name fields) of the user along with page headers(can be omitted).

I am not sure whether you meant java or Javascript. Please be specific on it.

I am a C# programmer basically. Will provide you a code to execute the above request in C# here.

WebRequest webRequest = WebRequest.Create("https://graph.facebook.com");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-UrlEncoded";
byte[] buffer = Encoding.UTF8.GetBytes("batch=[{'method': 'GET', 'relative_url': 'me'}, {'method': 'GET', 'relative_url': 'me/friends?limit=50'}]&access_token=@ACCESSTOKEN");
webRequest.ContentLength = buffer.Length;
using (Stream stream = webRequest.GetRequestStream())
{
    stream.Write(buffer, 0, buffer.Length);
    using (WebResponse webResponse = webRequest.GetResponse())
    {
        if (webResponse != null)
        {
            using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
            {
                string data = streamReader.ReadToEnd();
            }
        }
    }
}

Here the variable data will contain the result.

like image 23
Robin Avatar answered Oct 31 '22 17:10

Robin


Salah, here is the example i use as reference, i am sorry though i do not remember where i found.

FB.api("/", "POST", {
    access_token:"MY_APPLICATION_ACCESS_TOKEN",
    batch:[
        {
            "method":"GET",
            "name":"get-photos",
            "omit_response_on_success": true,
            "relative_url":"MY_ALBUM_ID/photos"
        },
        {
            "method": "GET",
            "depends_on":"get-photos",
            "relative_url":"{result=get-photos:$.data[0].id}/likes"
        }
    ]
}, function(response) {
    if (!response || response.error) {
        console.log(response.error_description);
    } else {    
        /* Iterate through each Response */
        for(var i=0,l=response.length; i<l; i++) {
            /*  If we have set 'omit_response_on_success' to true in the Request, the Response value will be null, so continue to the next iteration */
            if(response[i] === null) continue;
            /*  Else we are expecting a Response Body Object in JSON, so decode this */
            var responseBody = JSON.parse(response[i].body);
            /*  If the Response Body includes an Error Object, handle the Error */
            if(responseBody.error) {
                // do something useful here
                console.log(responseBody.error.message);
            } 
            /*  Else handle the data Object */
            else {
                // do something useful here
                console.log(responseBody.data);
            }
        }
    }
});
like image 1
ShawnDaGeek Avatar answered Oct 31 '22 16:10

ShawnDaGeek