开发者

Facebook php api not authenticating user? can someone show their working login code?

开发者 https://www.devze.com 2023-04-04 12:24 出处:网络
New to facebook api development.. taking a crack at php api and just trying to get user logged into facebook to show up... so far unsuccessfully.. I\'ve tried the different ways below to get a fb user

New to facebook api development.. taking a crack at php api and just trying to get user logged into facebook to show up... so far unsuccessfully.. I've tried the different ways below to get a fb user authenticated to no avail. How can I get the user ID to read? Can someone post just this short section of their code to help me see how you got this working? Thanks!


<?php

require 'facebook.php';

$facebook开发者_如何学C = new Facebook(array(
  'appId'  => 'zzzzzz',
  'secret' => 'yyyyyy',
));

// See if there is a user from a cookie
$user = $facebook->getUser();

?>

<?php

require 'facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
    'appId'  => 'zzzzzz',
    'secret' => 'xxxxxx',
  'scope'  => 'manage_pages,offline_access,publish_stream'
));

// Get User ID
$user = $facebook->getUser();


?>

<?php

require_once( 'lib/fb/src/facebook.php' );

$appId = 'zzzzzz';
$secret = 'yyyyyy';

$facebook = new Facebook( $appId, $secret );

// Get User ID
$user = $facebook->require_login();

?>


the sample code works from the php sdk is perfect!

<pre>
require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '191149314281714',
  'secret' => '73b67bf1c825fa47efae70a46c18906b',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>

</pre>


you must redirect the user to authenticate in login page of facebook.

$facebook = new Facebook(array(
  'appId'  => '...',
  'secret' => '...',
));
$user = $facebook->getUser();
    if ($user) {
         //user authenticate
        $logoutUrl = $facebook->getLogoutUrl();
        echo '<a href="$logoutUrl">loggout</a>';
         $friends =  $facebook->api('/me/'); //you profile details.. 
    } else {
      $loginUrl = $facebook->getLoginUrl();
       echo  '<a href="$loginUrl">Login with Facebook</a> to access this application.';
    }


You can check out some complete examples in the new PHP SDK documentation; the api method in particular may be of interest.

0

精彩评论

暂无评论...
验证码 换一张
取 消