Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook authentication in function - getUser() returns 0

I'm trying to make a function in my controller to post an album to Facebook. Every time the function is accessed, I'm redirected to REDIRECT_URI, as if the $userid=0, even if I just correctly logged into Facebook. Is this because after logging in, it restarts the function and creates a new Facebook session, wiping out the user I just got?

Thanks for any help you can provide.

 function share($id){
            if (!$id) { $this->Session->setFlash(__('Invalid id for Album', true)); 
            $this->redirect(array('action'=>'index')); }  
            $photos = $this->Album->find('all', array('conditions' => array('Album.id' => $id))); 


            $facebook = new Facebook(array('appId'=>'valid aphid', 'secret'=>'valid secret')); 
            $facebook->setFileUploadSupport(true);
            $userid = $facebook->getUser(); 

            if($userid) {               
                try {
                      $user_profile = $facebook->api('/me'); // Gets User's information based on permissions the user has granted to your application.

                } 
                catch(FacebookApiException $e){
                    error_log($e);
                    // Print results if you want to debug.
                    $userid = null;
                }

            } else {

                $login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => PERMISSIONS_REQUIRED));
                echo ("<script> top.location.href='".$login_url."'</script>");
            }

            //Create album
            $results = $facebook->api('me/albums', 'post', $photos[0]['Album']['name']);//->photos_createAlbum($albumname, '', '', 'everyone', $this->_userid); 

        }
like image 611
kyle Avatar asked Sep 09 '11 20:09

kyle


3 Answers

By default the Facebook class uses PHP sessions to store the authentication state of the user. There are four session variables that may be used:

  • fb_{appid}_user_id: Facebook user ID
  • fb_{appid}_code: authorization code that needs to be exchanged for an access token
  • fb_{appid}_access_token: access token that can be used to make API calls
  • fb_{appid}_state: CSRF token

Check that your PHP sessions are configured and working correctly.

  • By default the cookie name is PHPSESSID; you should see that being set in your browser.
  • If you are load balancing across multiple servers you will need a solution that lets you share session state across those servers.
  • You cannot output any text before starting the session (done automatically if needed when instantiating the Facebook object). You might want to try putting this as the very first line of your function:

    if (!session_id()) { session_start(); }
    
like image 109
PCheese Avatar answered Oct 27 '22 20:10

PCheese


I was having the exact same problem on my Facebook app, and I finally figured it out after 2 days of hair pulling frustration. It turned out to be an issue with the redirect-uri in the getLoginUrl()! if it doesn't match the registered app domain through facebook, they return the error, and the user gets returned as 0 (the default user value)

like image 22
Nick Avatar answered Oct 27 '22 18:10

Nick


May be a little late but I had the same sort of problem.

my error was the callback url it was missing the php file name: index.php I only gave http://www.xxxxx.xxx/ after i changed it to http://www.xxxxx.xxx/index.php it worked.

like image 26
mewiki Avatar answered Oct 27 '22 19:10

mewiki