开发者

Facebook api('/me') not authenticating

I've recently been trying to get my app working with the PHP SDK 3.0 and been having some trouble with it seeing that I am actually logged in. I have the "example.php" from the 3.1.1 download that doesn't acknowledge that I'm logged in. The file "with_js_sdk.php" also doesn't acknowledge that I'm logge开发者_如何学Pythond in when you first run it. After it has been run, however both that file and the "example.php" will see that I'm logged in. Once I log out, "example.php" doesn't work and "with_js_sdk.php" fails on the first run, but all subsequent runs both files (and some other files I'm testing this with) will all authenticate correctly. When not working, all files give an "OAuthException" with the message: "An active access token must be used to query information about the current user." even when I'm logged in. So my question is, is there something in the "with_js_sdk.php" that I can set in my other files? I'm guessing it has something to do with the "oauth:true" in that file from what I've read, although I'm not a Facebook coding expert so I'm not sure. And I have an iframe app, so I don't have the "FB.init" part in my application (that worked incidentally until the change in the sdk). Here's the minimum code I'm testing that's giving the error:

<?php

require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '11111111111111111',
  'secret' => 'abcdefghijk etc...',
  'cookie'=>true
));

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

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}
?>

The getuser always comes back correctly, but the $facebook->api('/me') call always fails until I run a file with the fbml in it. And before you ask, yes I changed the appID and secret in the files. Thanks.

Darryl


Instead of me pass $user. Eg:

$user_profile = $facebook->api('/'.$user);


I had the same problem and suffered for the longest time, and solved it by simply getting the new PHP SDK files from https://github.com/facebook/php-sdk.

Reference: http://developers.facebook.com/docs/reference/php/


Have you tried print_r($user_profile) to see what comes out? Also, the cookie parameter is not used with the latest SDK.


You have to be aware of the scope of variables, $user_profile will be only inside the scope of you if statement, so If you want to access from outer scope it will be not available remember you can always declare your variable outside and then initialize it, this way it will be available through your php file.

?php

require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '11111111111111111',
  'secret' => 'abcdefghijk etc...',
  'cookie'=>true
));

// See if there is a user from a cookie
$user = $facebook->getUser();
$user_profile; // variable being declare outer scope
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}
?>


THIS WORKS FOR ME

$user_profile = $facebook->api('/'.$user);

And here is my full php fb connect script example: `

$fbconfig['appid' ] = " id ";
$fbconfig['secret'] = " secret ";
$fbconfig['appBaseUrl'] =   "https://apps.facebook.com/ example /";

$user            =   null; //facebook user uid
try{
    include_once "facebook.php";
}
catch(Exception $o){
    echo '<pre>';
    print_r($o);
    echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
  'appId'  => $fbconfig['appid'],
  'secret' => $fbconfig['secret'],
  'cookie' => true,
));

//Facebook Authentication part
$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.

$loginUrl   = $facebook->getLoginUrl(
        array(
            'scope'         => 'email,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown'
        )
);

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $me = $facebook->api('/'.$user);
  } catch (FacebookApiException $e) {
    //you should use error_log($e); instead of printing the info on browser
    d($e);  // d is a debug function defined at the end of this file
    $user = null;
  }
}

if (!$user) {
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    exit;
}

//get user basic description
$me = $facebook->api("/$user");

function d($d){
    echo '<pre>';
    print_r($d);
    echo '</pre>';
}

if (isset($_GET['code'])){
        header("Location: " . $fbconfig['appBaseUrl']);
        exit;
    }`

Have a nice day ! :)


when the token is invalid, kick off the authentication flow.

also, to get jssdk to work with php sdk v3.x, make sure you have upgrade your js sdk code to use OAuth 2.0.


you are missing with something.To have an active access token you must present it inside a session variable.And you are not even creating a session in your code. YOU MUST HAVE AN ACCESS TOKEN FIRST try this-> $access_token =$facebook->getAccessToken(); if ($user) { if( session_id() ) {} else {session_start();}

Now you should store $_SESSION['access_token']=$access_token ; for later use.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜