与facebook的oauth

时间:2012-03-20 15:02:14

标签: php facebook facebook-oauth

如果用户在Facebook中登录,我试图获取,如果是,那么我需要在我的数据库中存储他们的个人资料图片网址,以便用户可以在我的网络应用程序中发表评论。

我无法做到这一点,我为网站创建了一个facebook应用程序,并使用以下代码进行授权。但它显示“发生错误。请稍后再试。”

任何人请帮帮我?还是帮我处理代码?

<?php
require_once('../src/facebook.php');

$fb_app_id = "key";
$fb_secret = "secret";
$fb_app_url = "url";
$facebook = new Facebook(array(
    'appId'  => $fb_app_id,
    'secret' => $fb_secret,
    'cookie' => true,
));

$user = $facebook->getUser();

if ($user) {
  // The user is logged in
  try {
    $user_profile = $facebook->api('/me');
    // Here : API call succeeded, you have a valid access token
  } catch (FacebookApiException $e) {
    // Here : API call failed, you don't have a valid access token
    // you have to send him to $facebook->getLoginUrl()
    $user = null;
  }
} // else : the user is not logged in


?>



<?php if ($user): ?>
    <a href="<?php echo $facebook->getLogoutUrl() ?>">Logout of Facebook</a>
<?php else: ?>
    <a href="<?php echo $facebook->getLoginUrl() ?>">Login with Facebook</a>
<?php endif ?>

1 个答案:

答案 0 :(得分:1)

你可以试试这个

require_once('../src/facebook.php');
$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxx',
  'cookie' => true,
));

$fb_user_id = $facebook->getUser();

try 
{
$fb_user_profile = $facebook->api('/me');   
$me  = $facebook->api('/me');
}
catch (FacebookApiException $e) 
{

$fb_user_id = NULL;
}
$fbid = $me['id']; 
$firstname = $me["first_name"];
$lastname =  $me["last_name"];
$gender = $me["gender"];
$email = $me["email"];

if ($facebook->getSession()) {
  echo '<div id="fbc-img"><img src="http://graph.facebook.com/'.$me['id'].'/picture?type=normal" width="56" height="56"></div>
  <div id="fbc-info">Welkom, '.$firstname.' ! <br><a href="' . $facebook->getLogoutUrl() . '">Logout </a></div> ';

} else {
  echo '<a href="https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_WEBSITE&scope=email" target="_blank">Login met facebook</a> ';
}