开发者

Can't getUser after using Facebook registration plugin

So I'm using the Facebook registration plugin along with the Javascript SDK (to display it using xfbml). The registration works fine and my code to process it looks like this:

# Check if the form has been submitted.
if(array_key_exists('signed_request', $_REQUEST))
{
    define('FACEBOOK_APP_ID', 'REMOVED');
    define('FACEBOOK_SECRET', 'REMOVED');

    function parse_signed_request($signed_request, $secret) {
        list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

        // decode the data
        $sig = base64_url_decode($encoded_sig);
        $data = json_decode(base64_url_decode($payload), true);

        if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
            error_log('Unknown algorithm. Expected HMAC-SHA256');
            return null;
        }

        $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
        if ($sig !== $expected_sig) {
            error_log('Bad Signed JSON signature!');
            return null;
        }

        return $data;
    }

    function base64_url_decode($input) {
        return base64_decode(strtr($input, '-_', '+/'));
    }

    if ($_REQUEST) {
        # Load the Users model.
        $this->load->model('Users');

        # Save the data from the form.
        $response = parse_signed_request($_REQUEST['signed_request'], FACEBOOK_SECRET);

        # Save the user's email.
        $user['email'] = $response['registration']['email'];

        # Boolean that stores if the user registered through FB.
        $facebook;

// the rest is irrelevant

So once the user is registered I redirect them back to the home page. When I log into my Facebook account, my website shows up under the apps I've used (开发者_如何转开发in the privacy settings).

Despite that, the user doesn't appear authenticated when I try to use the PHP SDK on the home page. The following code prints out "0" as the $user_id and "not valid" as $isvalid. The following code is for the home page:

require 'application/sdk/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'REMOVED',
  'secret' => 'REMOVED',
));

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

echo $user;
$isvalid;

try { 
    $facebook->api('/me');
    $isvalid = true;
} catch (FacebookApiException $e) {
    $isvalid = false;
    echo "not valid";
}

What am I doing wrong?

Thanks!


After pulling my hair out for the past eight hours, I found this gem on the bottom of a Facebook developer blog post:

If you are only using the PHP SDK for authentication, please upgrade now. If you are using the JavaScript SDK for login in conjunction with the PHP SDK, you will want to wait for the JavaScript SDK upgrade (coming in 4 weeks). Version 3.0.0 of the PHP SDK won’t cooperate with the current JavaScript SDK due to the cookie format changing.

Time to use the old SDK!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜