简体   繁体   中英

oauth with facebook

I am trying to get if user is logined in facebook, if yes then I need to store their profile pic url, name in my db, so that users can comment in my web application.

I cant do this, I created a facebook application for website and using the following code for auth purpose. But its showing "An error occurred. Please try again later."

Any one please help me ? or help me with working code ?

<?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 ?>

You can try this

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> ';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM