Developers Manual > SDK Integration > Facebook > Facebook API References in JavaScript > Facebook-graph-api

.api(path, method, params, callback)

Makes a call to the Facebook Graph API to get data, or take action on a user's behalf. This will almost always be used once a user is logged in, and an access token has been granted; the permissions encoded by the access token determine which Graph API calls will be available.

Parameters

1
plugin.FacebookAgent.prototype.request = function(path, method, params, callback){}
Name Type Required Description
endpoint String Yes The Graph API endpoint to call
method plugin.FacebookAgent.HttpMethod Yes The
params Object No The key/value pairs to be passed to the endpoint as arguments. For details of arguments for each endpoint, please refer to Graph API Reference
callback Function No Callback function containing a result code and a JSON response.

Callback function

1
var callback = function (code, response) {}

If the API call is successful, code is plugin.FacebookAgent.CODE_SUCCEED, otherwise, code is error code and response is a JSON containing error message as the example below,

1
2
3
4
// The response object 
{
    "error_message": "Unknown error message"
}

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var facebook = plugin.FacebookAgent.getInstance();

// Retrieve user id
facebook.api("/me", plugin.FacebookAgent.HttpMethod.GET, function (type, response) {
    if (type == plugin.FacebookAgent.CODE_SUCCEED) {
        cc.log(response["id"]);
    } else {
        cc.log("Graph API request failed, error #" + code + ": " + response);
    }
});

// Send a photo to user's photo album
facebook.api("/me/photos", plugin.FacebookAgent.HttpMethod.POST, {"url": "http://files.cocos2d-x.org/images/orgsite/logo.png"}, function (type, response) {
    if (type == plugin.FacebookAgent.CODE_SUCCEED) {
        cc.log("post_id: " + response["post_id"]);
    } else {
        cc.log("Graph API request failed, error #" + code + ": " + response);
    }
});

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.