1

我想在我的网站上展示我的公开活动。我使用下面的代码,但它返回一个空对象。

我的应用程序具有以下权限: user_events; rsvp_event, read_stream 我不会使用“offline_access”,它将被弃用权限。

我广泛阅读了 api 文档,但它缺乏实际的例子......

当我使用“静态令牌/$token_offline”时,它会正确返回事件。

// 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) {
  }
}
*/

我将此代码用作临时解决方案,并想知道社区对此方法的看法。

// 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;
}
4

1 回答 1

1

您需要请求用户访问令牌,而不是您现在请求的应用程序访问令牌。阅读带有示例的材料:https ://developers.facebook.com/docs/authentication/server-side/

于 2012-05-02T12:00:33.907 回答