2012-05-02 37 views
1

我想在我的網站上展示我的公共活動。 我使用下面的代碼,但它返回一個空對象。獲取我的Facebook公開活動並使用Facebook API在我的網站上展示 - SDK

我的應用程序有權限: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) { 
    } 
} 
*/ 

我正在使用此代碼作爲臨時解決方案,並希望知道該方法的社區opnion。

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

回答

1

您需要申請一個用戶訪問令牌,而不是您現在請求的應用訪問令牌。閱讀材料的一個例子:https://developers.facebook.com/docs/authentication/server-side/

+0

感謝您的幫助。正是在這一段你給我的鏈接,我清除了思想: 「 你應該解析這個字符串並使用的access_token值,使請求的圖形API你也應該堅持的訪問令牌在數據庫中爲了進一步請求的API,而無需重新驗證用戶..... 「 這使我這個其他鏈接: 去除offline_access許可 https://developers.facebook.com/路線圖/離線存取去除 再認證 https://developers.facebook.com/docs/authentication/reauthentication –

相關問題