Use the following code to generate the code; Once you have your access code, you can exchange it for an extended code. The last part of the code shows how to debug:
$app_id = "XXXXXXXXX";
$app_secret = "YYYYYYYYYYYY";
$redirect_url = "http://www.example.com/page.php";
$fb_code = $_REQUEST['code'];
if(!$fb_code)
{
$login_dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $redirect_url)
. "&scope=read_friendlists,read_stream,xmpp_login,user_online_presence,friends_online_presence,create_event,publish_stream&response_type=code";
echo("<script>location.href='" . $login_dialog_url . "'</script>");
}
else
{
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=". urlencode($redirect_url)
. "&client_secret=" . $app_secret
. "&code=" . $fb_code;
$response = file_get_contents($token_url);
parse_str($response, $params);
$access_token = $params['access_token'];
echo 'Access Token: '.$access_token.'<br>';
Generate extende code here if required
if($access_token)
{
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=". urlencode($redirect_url)
. "&client_secret=" . $app_secret
. "&grant_type=fb_exchange_token"
. "&fb_exchange_token=" . $access_token;
$response = file_get_contents($token_url);
parse_str($response, $params);
$extended_access_token = $params['access_token'];
echo 'Extended Access Token: '.$extended_access_token;
}
Debug Extended Code here:
$expires = "https://graph.facebook.com/debug_token?input_token=$access_token&access_token=$extended_access_token";
$response = file_get_contents($expires);
echo "<pre>";
print_r(json_decode($response));
echo "</pre>";