Facebook Graph API returns false

后端 未结 2 1628
野性不改
野性不改 2020-12-18 06:56

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

相关标签:
2条回答
  • 2020-12-18 07:15

    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

    0 讨论(0)
  • 2020-12-18 07:27

    Try it without the last slash:

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

    Also try it with an application access token.

    0 讨论(0)
提交回复
热议问题