简体   繁体   中英

user id not returning using facebook php sdk

$this->_facebookObj = new Facebook($config); 
try
{
   $this->_userId = $this->_facebookObj->getUser();
}
catch(Exception $e)
{
}

I've tried to run this sample code on my local machine with proper application id and secret key. But response shows like I need an active access token. So i logged in to the Facebook, then i get the user id successfully. but whenever I logged out from Facebook I couldn't run the above sample code. I think its because of session.

How do we run above API when the user's logged in session not available?

I tried the following :

$this->_facebookObj->getAccessToken();
$this->_facebookObj->setAccessToken(); 

before making the getUser() API.

You should take a look at this answer as it explains how the authentication system work.

That said, here is how you can get the userId :

$facebook = new Facebook($config);

if (!$facebook->getUser())
{
    // Cannot access the user account
}

// try to access the user on the fb api
try
{
    $fbUser = $facebook->api('/me');
    $id = $fbUser['id'];
}
catch (\Exception $e)
{
    // Session expired, ask the user to log in again...
}

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