简体   繁体   中英

Facebook PHP SDK getUser works with canvas app on Facebook but in mobile site it doesn't work

I've made a Facebook app which works perfectly, so now I'm trying to setup a mobile app. I use this code with is described in the docs to check if the user is logged in or not, and if the user is logged in pass the information forward.

Here is the code I'm using:

<?php 
session_start();

// Include facebook PHP SDK
 require_once("lib/facebook.php");
 

// Configure the facebook object
$config = array();
$config['appId'] = 'APP_ID';
$config['secret'] = 'APP_SECRET';
$config['cookie'] = true;
$config['fileUpload'] = true;

$app_id = 'APP_ID';
$canvas_page = "https://hadadmin.yourwebhosting.com/mixit/mobile/";

     $location = "https://www.facebook.com/dialog/oauth?client_id=" 
        . $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=publish_stream,email,create_event,rsvp_event,user_events,friends_events";

    // initialize the facebook oject
  $facebook = new Facebook($config);

    // Get Facebook User
    $fbID = $facebook->getUser();
 
    // Get the current access token
    $access_token = $facebook->getAccessToken();
    
    // check if we have valid user
if ($fbID) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $fb_user_profile = $facebook->api('/me');   

    } catch (FacebookApiException $e) {
        $fbID = NULL;
        // seems we don't have enough permissions
        // we use javascript to redirect user instead of header() due to Facebook bug
        print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';

        // kill the code so nothing else will happen before user gives us permissions
        die();
    } 

} else {
    // seems our user hasn't logged in, redirect him to a FB login page

print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';

  // kill the code so nothing else will happen before user gives us permissions
   die();
}

I've searched all over but none of the solutions to similar problems are working (ie getUser returns 0) getUser is returning 0 like in the other posts, but like I said it works fine when I don't use the mobile site, apps.facebook.com/MYAPP and https://hadadmin.yourwebhosting.com/mixit/ is also working.

getUser use cookie setup by the javascript SDK or via the signed_request (for canvas and page tab).

As you're on a mobile site and if it's the first user connection, you'll have no cookie, and obvisously no signed_request . So you need to setup the Facebook connection using the javascript SDK by settings status to true in the init method, or by calling getLoginStatus . Then, you can request your server (the cookie will be send with any ajax request on the same domain).

after edit: From what I see, you have trouble with cross-domain communication here. Be sure to setup facebook correctly with a valid channel file. For IE, setup P3P headers, that will help a lot. For Safari, that's a hell of a job, but google "cross-domain communication" and you'll find a lot of solutions. But these shouldn't be necessary if Facebook SDKs are setup correctly.

Hope this help!

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