2015-12-29 38 views
2

我打算創建一些非交互式的,基於服務器的PHP網絡邏輯,從Facebook檢索信息。作爲使用Facebook SDK的初學者,我有問題入門。Facebook的SDK和PHP的 - 服務器端身份驗證和訪問

純粹基於服務器,我不能假設我的用戶有Facebook帳戶,所以我想爲他們檢索FB信息 - 無論是使用靜態帳戶還是匿名。

使用我已經建立了我的App ID和App祕密的FB儀表板,並安裝SDK Facebook的PHP-SDK-v4-5.0.0

現在,我已經麻煩/理解我FB access_code正確的(也可能不止於此):

define('APP_NAME',   "xxxxx"); //  
define('APP_ID',   "19427xxxxxxx"); 
define('APP_VERSION',  "v2.5"); 
define('APP_SECRET',  "8c31fxxxxxxx"); 

session_start(); 

$fb = new Facebook\Facebook([ 
    'app_id'     => APP_ID, 
    'app_secret'    => APP_SECRET, 
    'default_graph_version' => APP_VERSION, 
]); 

// get oauth token ?? 
$query = http_build_query([ 
    'client_id'  => APP_ID, 
    'client_secret' => APP_SECRET, 
    'grant_type' => 'client_credentials', 
]); 

$response1 = $fb->get('/oauth/access_token?' . $query, $fb->getApp()->getAccessToken()); 
$graph = $response1->getGraphObject();  
foreach($graph->getPropertyNames() as $prop) 
{ 
    echo $prop, "=", $graph->getProperty($prop), "<br/>", "\n"; 
} 

$access_token = $graph->getProperty('access_token'); 
$fb->setDefaultAccessToken($access_token); 

$request = new Facebook\FacebookRequest($fb->getApp(), $access_token, 'GET', '/me?fields=id,name'); 

// Send the request to Graph 
try 
{ 
    $response = $fb->getClient()->sendRequest($request); 
} 
catch (Facebook\Exceptions\FacebookResponseException $e) 
{ 
    echo 'Graph returned an error: ' . $e->getMessage();  
    // --> Invalid OAuth access token. 
    // --> An active access token must be used to query information about the current user 
    exit; 
} 

看來我只是不明白的access_code權利,因爲無論我做什麼,我得到任何「無效OAuth訪問令牌」或「必須使用的主動訪問令牌查詢有關當前用戶的信息「

任何洞察力都很muc h讚賞。

更新 - 這終於爲我工作:

session_start(); 

$fb = new Facebook\Facebook([ 
    'app_id'     => APP_ID, 
    'app_secret'    => APP_SECRET, 
    'default_graph_version' => APP_VERSION, 
]); 

// get oauth token ?? 
$query = http_build_query([ 
    'client_id'  => APP_ID, 
    'client_secret' => APP_SECRET, 
    'grant_type' => 'client_credentials', 
]); 

$r_token = $fb->get('/oauth/access_token?' . $query, $fb->getApp()->getAccessToken()); 
$graph= $r_token->getGraphObject();  
$a_token= $graph->getProperty('access_token'); 
$fb->setDefaultAccessToken($a_token); 
$since = strtotime($since_date); 
$until = strtotime($until_date);   
$fields= "id,name,description,place,timezone,start_time,end_time"; 
$json_link= "https://graph.facebook.com/" . PAGE_ID . "/events/attending/?access_token={$a_token}&fields={$fields}&since={$since}&until={$until}"; 
$json = file_get_contents($json_link);  
+0

我不認爲你可以使用一個APP訪問令牌與用戶相關的API請求。您必須使用USER訪問令牌(通常您通過與facebook登錄進行獲取,因此用戶可以授予您的應用程序權限)。 – cwps

+0

@cwps:我如何在PHP服務器上下文中登錄Facebook(例如,使用我的應用程序祕密或使用硬編碼憑據)來獲取我的訪問令牌?如上所述,我不想假設我的服務的消費者有fb賬戶。 –

+0

「_...從Facebook檢索信息。」< - 您想要檢索什麼樣的信息?你的Facebook頁面?你的應用頁面?或者是什麼?從你的代碼:''GET','/ me?fields = id,name'',這表示獲得用戶ID和名稱,所以你必須擁有該用戶訪問令牌(即使用戶是你自己),你可以不要使用你的APP訪問令牌。 – cwps

回答

0

這終於爲我工作:

session_start(); 

$fb = new Facebook\Facebook([ 
    'app_id'     => APP_ID, 
    'app_secret'    => APP_SECRET, 
    'default_graph_version' => APP_VERSION, 
]); 

// get oauth token ?? 
$query = http_build_query([ 
    'client_id'  => APP_ID, 
    'client_secret' => APP_SECRET, 
    'grant_type' => 'client_credentials', 
]); 

$r_token = $fb->get('/oauth/access_token?' . $query, $fb->getApp()->getAccessToken()); 
$graph= $r_token->getGraphObject();  
$a_token= $graph->getProperty('access_token'); 
$fb->setDefaultAccessToken($a_token); 
$since = strtotime($since_date); 
$until = strtotime($until_date);   
$fields= "id,name,description,place,timezone,start_time,end_time"; 
$json_link= "https://graph.facebook.com/" . PAGE_ID . "/events/attending/?access_token={$a_token}&fields={$fields}&since={$since}&until={$until}"; 
$json = file_get_contents($json_link);  
相關問題