简体   繁体   中英

Verify/validate User access for android app using facebook SDK at server side in PHP?

I have a android app. User can access to the app using facebook login. Once user logged in, his all data/activity will push to web server using php. I just wanted to validate user access for app while he accessing the app. Just wanted to check user app access status using his access token when his request comes to php web server. The main aim is to deny fake users request from android to web server for the security purpose. Can anyone help me to sort out my problem . Thanks in advance..

<?php 
try{
    require_once(dirname(__FILE__) . '\facebook-php-sdk\src\Facebook\facebook.php' );
}
catch(Exception $o){
        print_r($o);
}
$config = array(
    'appId' => '123456',
    'secret' => '5ca2b11ea4c8sdsdsd',
    'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);

$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$user_id = '123456';

if($user_id) {

    // We have a user ID, so probably a logged in user.
    // If not, we'll get an exception, which we handle below.
    try {
        $ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
            array(
                'link' => 'www.example.com',
                'message' => 'Posting with the PHP SDK!'
            ));

        // $ret_obj = $facebook->api('/me/feed', 'POST',
        //     array(
        //         'access_token' => $facebook->getAccessToken(),
        //         'link' => 'www.example.com',
        //         'message' => 'Posting with the PHP SDK!'
        //     ));
        echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

        // Give the user a logout link
        echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
    } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        echo "<pre>";var_dump($e);
        $login_url = $facebook->getLoginUrl( array(
            'scope' => 'publish_stream'
        ));
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
    }
} else {

    // No user, so print a link for the user to login
    // To post to a user's wall, we need publish_stream permission
    // We'll use the current URL as the redirect_uri, so we don't
    // need to specify it here.
    $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
    echo 'Please <a href="' . $login_url . '">login.</a>';

}?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM