Rentringer
Rentringer

Reputation: 61

Get my facebook public events and show in the my site with Facebook API - SDK

I'd like to show my public events on my site. I use the code below, but it returns an empty object.

My app has permissions on: user_events; rsvp_event, read_stream I would not use "offline_access" it will be deprecated permission.

I read extensively the api documentation, but it lacks practical examples...

As I use the "static token / $token_offline" it returns the events correctly.

// include SDK
if (!class_exists('Facebook'))
    require_once (THEME_FACEBOOK_SDK . '/src/facebook.php');

// params config
define('PROFILE_ID', 'XXXXXXXXXXX'); // my profile id
define('APP_ID', 'XXXXXXXXXXX'); // my application APP_ID in facebook
define('APP_SECRET', 'XXXXXXXXXXX'); // my application APP_SECRET in facebook

// create application instance
$facebook = new Facebook(array('appId' => APP_ID, 'secret' => APP_SECRET, 'cookie' => true));

// get graph API
function fetchUrl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $retData = curl_exec($ch);
    curl_close($ch);
    return $retData;
}

/*--- I do not want to use it (Generated by Graph API Explorer)
$token_offline = 'AAAD5V3tZBKEQBAAmAfxTb8ZAObX3h6LfSTZCilFneZCkc5YEF5T0SjLt6ZB9i7wQUuvaqhIandqzkiHivZAjYyzMsOiajPsNYZD';*/

// get app token
$graph_url = 'https://graph.facebook.com/oauth/access_token?client_id=' . APP_ID . '&client_secret=' . APP_SECRET . '&grant_type=client_credentials';

// app token
$app_token = fetchUrl($graph_url);    

// get events
$graph_url = 'https://graph.facebook.com/' . PROFILE_ID . '/events?' . $app_token; //$token_offline

var_dump(json_decode(fetchUrl($graph_url)));

/*
object(stdClass)#3947 (1) {
  ["data"]=>
  array(0) {
  }
}
*/

I am using this code as a temporary solution and would like to know the community opnion on this method.

// Get response Graph API. Note this wrapper function exists in order to circumvent PHP’s strict obeying of HTTP error codes. In this case, Facebook returns error code 400 which PHP obeys and wipes out the response.
function fetchUrl($URL) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    $err = curl_getinfo($c, CURLINFO_HTTP_CODE);
    curl_close($c);
    if ($contents)
        return $contents;
    else
        return false;
}

// In the start config only. Save in database the token generated by tools Graph API Explorer
//update_option('access_token_fb', 'AAAD5V3tZBKEQBAGTScYxCZAt3bWXJvzimsfWYAG0SWF40ffngLF03dU8T63gXWHJuiXLZClSoxzuC5UZAsXNDK5yQkQqBPXgAuJPZA3ZBjlgZDZD');

function get_events_facebook(){
    $profile_id = 'XXXXXXXXXXX'; // user id my profile facebook
    $app_id = 'XXXXXXXXXXX'; // app id 
    $app_secret = 'XXXXXXXXXXX'; // app secret

    // events user select
    $fql = "SELECT eid, name, pic, start_time, end_time, location, description FROM event WHERE creator={$profile_id} AND eid IN ( SELECT eid FROM event_member WHERE uid={$profile_id} ) ORDER BY start_time asc";

    // link graph API get events
    $api_url = 'https://graph.facebook.com/fql?q=' . urlencode($fql);

    // get token saved in the database 
    if($access_token = get_option('access_token_fb')) {

        // get events based in token saved in database
        $decoded_response = json_decode(fetchUrl($api_url . '&access_token=' . $access_token));

        // if response returned is error get new token based in old token database
        if (isset($decoded_response->error) && $decoded_response->error->type == "OAuthException") {

            // Client-side OAuth and Extending Access_Token Expiration Time through New Endpoint 
            $refresh_token = 'https://graph.facebook.com/oauth/access_token?client_id=' . $app_id . '&client_secret=' . $app_secret . '&grant_type=fb_exchange_token&fb_exchange_token=' . $access_token;

            // create variable $token and set token value
            parse_str(fetchUrl($refresh_token));

            // save new token in database
            update_option('access_token_fb', $access_token);

            // get events based in the new token
            return json_decode(fetchUrl($api_url . '&access_token=' . $access_token));
        }

        // events returned based in ancient token saved in database
        return $decoded_response;
    }

    // if no token saved in database
    return false;
}

Upvotes: 1

Views: 1735

Answers (1)

Yan Berk
Yan Berk

Reputation: 14438

You need to request a user access token instead of the app access token you are requesting now. Reading material with an example: https://developers.facebook.com/docs/authentication/server-side/

Upvotes: 1

Related Questions