2017-01-02 116 views
1

我需要將一些帖子從Facebook頁面中取出並顯示在我的網站上。我正在考慮使用Facebook Graph API來實現這一點,但我正在努力一點,文檔和各種教程似乎都假設我將設置一個登錄流程,首先要求用戶登錄到我的網站。但那不是我需要的。我需要我的服務器端應用程序(保持)與Facebook進行身份驗證,並定期獲取任何新帖子。使用Facebook Graph API進行身份驗證

我發現應該可以通過使用頁面標記來獲得長壽命的標記,但現在我甚至無法弄清楚如何使用普通的短命令標記進行身份驗證。

因此,我設置了一個測試應用程序來獲取應用程序ID和密碼,並前往Facebook Graph API Explorer以獲取用於創建應用程序的用戶的身份驗證令牌。

下面是基本的代碼我使用的測試連接圖形API:

(...) 
$fb = new \Facebook\Facebook([ 
    'app_id' => $app_id, 
    'app_secret' => $app_secret, 
    'default_graph_version' => '2.8', 
    'default_access_token' => $app_access_token 
]); 

try { 
    $response = $fb->get('/me'); 
} catch(\Facebook\Exceptions\FacebookResponseException $e) { 
    echo 'Graph returned an error: ' . $e->getMessage(); 
    exit; 
} catch(\Facebook\Exceptions\FacebookSDKException $e) { 
    // When validation fails or other local issues 
    echo 'Facebook SDK returned an error: ' . $e->getMessage(); 
    exit; 
} 

$me = $response->getGraphUser(); 

當我運行我的代碼我得到這個消息:

Graph returned an error: Invalid appsecret_proof provided in the API argument

我三重檢查$app_id$app_secret是我在創建應用時給出的值,並且我已經三重檢查了我的訪問令牌(使用我的個人用戶令牌以及通過圖形API資源管理器可以訪問的頁面令牌)。

我發現了另一個線程SO暗示我應該手動生成appsecret證明,所以我這樣做:

$appsecret_proof= hash_hmac('sha256', $app_access_token, $app_secret); 

$fb = new \Facebook\Facebook([ 
    'app_id' => $app_id, 
    'app_secret' => $appsecret_proof, 
    'default_graph_version' => '2.8', 
    'default_access_token' => $app_access_token 
]); 

我不知道我是否應該通過$appsecret_proofapp_secret或者我應該如何使用它。我仍然在上面的代碼中遇到同樣的錯誤。

+0

一般來說,在使用官方sdk時,你不必處理appsecret_proof。如果沒有,您必須將其添加到每個API調用。 – luschn

+0

是的,據我所知,它不是強制性的,但鑑於錯誤消息告訴我'appsecret_proof'是無效的,我想明確使用它可能會有所幫助。但是我有或沒有得到相同的錯誤信息。 – funkylaundry

回答

0
get facebook mutual friend with app user or non app user 

    $access_token = "facebook user token"; 
    $facebook_id = "friend id"; 
    $app_secret = "facebook secret"; 
    $appsecret_proof = hash_hmac('sha256', $access_token, 
    $app_secret); 


    $graph_url = "https://graph.facebook.com/" . $facebook_id . "?fields=context.fields(all_mutual_friends.fields(id,name,picture.width(200).height(200)))" . "&access_token=" . $access_token . "&appsecret_proof=" . $appsecret_proof; 


    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $graph_url); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

    $output = curl_exec($ch); 
    return $response_mutual = json_decode($output, true); 
    curl_close($ch); 
相關問題