Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Graph API returns false

I am working with friend requests. When the user sends a friend request, I get a request_id as a response. However, when i use this request_id ( XXXX) as follows:

https://graph.facebook.com/XXXX/?access_token=YYYYYYYYY

it returns:

false

the access_token seems to be the right one( the one used here is for example only), am i missing something ? what does false mean? how do i get the JSON object as the return data?

like image 296
lugbug Avatar asked Feb 16 '11 00:02

lugbug


1 Answers

Seems like you have any restriction in your page config. I guess any restriction by country or by age.

Remove the condition in the FB page and try again.

UPDATE:

Is just my guessing but I assume that the reason because it return false in case of restrictions is because FB could not verify is the request complies with the page restrictions. I solve the problem using OAuth 2.0

Facebook docs for OAuth 2.0

And here some code to make the life easier:

FILE 1:

require_once( 'oauth2callback/index.php' );

// Get likes from FB using auth
$likes = do_auth_FB( '[Object name]' );
echo 'Likes en Facebook: ' . $likes . '<br/>';

FILE 2: (oauth2callback/index.php)

function do_auth_FB( $objectName) {

    // Setting required variables
    $fbAppID = '[App ID]';
    $fbSecret = '[App secret]';
    $redirectURI = '[URL to redirect]';
    $objectName = '[Object name]';

    // Getting code var
    $code = $_GET["code"];

    // Calling for code
    if( empty( $code ) ) {
        $urlcode = 'http://www.facebook.com/dialog/oauth?'
       . 'client_id=' . $fbAppID . '&redirect_uri=' . urlencode( $redirectURI )
       . '&client_secret=' . $fbSecret;

        echo "<script> top.location.href='" . $urlcode . "'</script>";
    }

    // Calling for token
    $urlToken = 'https://graph.facebook.com/oauth/access_token?'
       . 'client_id=' . $fbAppID . '&redirect_uri=' . urlencode( $redirectURI )
       . '&client_secret=' . $fbSecret . '&code=' . $code;

    $response = file_get_contents( $urlToken );
    $outputResponse = null;
    parse_str( $response, $outputResponse );

    // Calling for the required object
    $graph_url = 'https://graph.facebook.com/' . $objectName . '?access_token='
       . $outputResponse["access_token"];

    $objectStream = json_decode( file_get_contents( $graph_url ) );

    return $objectStream->likes;
}

In the example only returns the page likes

like image 185
Crazystress Avatar answered Oct 12 '22 19:10

Crazystress