2012-02-05 23 views
0

特定のファンページのストリームをサーバーから読み取る必要があります。 グラフを読もうとしました https://graph.facebook.com//feed?access_token = & limit = 100 これは機能します。 私が必要とするのは、トークンが期限切れになるか、それをプログラムで更新するかを理解することです。 http://developers.facebook.com/tools/explorer/アプリからトークンを生成しました。 私を助けてもらえますか? PHPのSDMを使用しています ありがとう、 a。Facebookページの投稿とトークンの有効期限を読む

答えて

1

あなたはコードの下に使用してFacebookのページを読むことができますし、また、指定したフィールドに

https://graph.facebook.com/$page_id/?fields=link,etc&access_token=page_access_token 

または

以下
$response = $fb->api($page_id . '/?fields=link,etc&'. $access_token, 'GET') 

を得ることができ、4つのシナリオ

ためのソリューションです1.トークンは有効期限が切れた後に期限切れになります(デフォルトは2時間です)。
2.ユーザーがパスワードを変更すると、アクセストークンが無効になります。
3.ユーザーがあなたのアプリケーションを認証解除します。
4.ユーザーはFacebookからログアウトします。

ユーザーにとって最高のエクスペリエンスを確保するには、上記のシナリオのエラーをキャッチする必要があります。次のPHPコードは、これらのエラーを処理し、新しいアクセストークンを取得する方法を示しています。

ユーザーを認証ダイアログにリダイレクトすると、ユーザーが既にアプリケーションを承認している場合、ユーザーはアクセス許可の入力を求められません。 Facebookはあなたにダイアログを向けるユーザなしであなたに有効なアクセストークンを返します。ただし、ユーザーがアプリケーションの認証を解除した場合、ユーザーはaccess_tokenを取得するためにアプリケーションを再認証する必要があります。詳細情報について

<?php 
$app_id = "YOUR_APP_ID"; 
$app_secret = "YOUR_APP_SECRET"; 
$my_url = "YOUR_POST_LOGIN_URL"; 

// known valid access token stored in a database 
$access_token = "YOUR_STORED_ACCESS_TOKEN"; 

$code = $_REQUEST["code"]; 

// If we get a code, it means that we have re-authed the user 
//and can get a valid access_token. 
if (isset($code)) { 
$token_url="https://graph.facebook.com/oauth/access_token?client_id=" 
    . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret 
    . "&code=" . $code . "&display=popup"; 
$response = file_get_contents($token_url); 
$params = null; 
parse_str($response, $params); 
$access_token = $params['access_token']; 
} 


// Attempt to query the graph: 
$graph_url = "https://graph.facebook.com/me?" 
. "access_token=" . $access_token; 
$response = curl_get_file_contents($graph_url); 
$decoded_response = json_decode($response); 

//Check for errors 
if ($decoded_response->error) { 
// check to see if this is an oAuth error: 
if ($decoded_response->error->type== "OAuthException") { 
    // Retrieving a valid access token. 
    $dialog_url= "https://www.facebook.com/dialog/oauth?" 
    . "client_id=" . $app_id 
    . "&redirect_uri=" . urlencode($my_url); 
    echo("<script> top.location.href='" . $dialog_url 
    . "'</script>"); 
} 
else { 
    echo "other error has happened"; 
} 
} 
else { 
// success 
echo("success" . $decoded_response->name); 
echo($access_token); 
} 

// 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 curl_get_file_contents($URL) { 
$c = curl_init(); 
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); 
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; 
} 
?> 

あなたはこのlink
おかげ

+0

おかげで多くを訪問することができます。このコードはユーザーベースの読み取りに最適です。可能であれば、ユーザーの介入なしにサーバー上でのみ動作するシステムが必要です。出来ますか? – Andrea

+0

@アンドレアしかしこれはFacebookの表記の言葉に対して –

+0

私はなぜ分かりません。この場合に適用されるルールを教えてください。どうもありがとう – Andrea

関連する問題