Facebook Php SDK - getUser() return always 0

2019-05-22 13:25发布

问题:

I read all threads on this forum but I can't find my solution..

I use the last Php SDK by facebook : v3.2.2 .

So I used this:

//start fb
$facebook = new \Facebook(array(  
    'appId'  => $fb_app_id,  
    'secret' => $fb_app_secret,  
    'cookie' => true,  
));

// get id user-fb
$fbUser = $facebook->getUser();

if ($fbUser) {
    try {
      $fbUserProfile = $facebook->api('/me'); // take user-infos
      $friends = $facebook->api('/me/friends'); // take his friend

    } catch (FacebookApiException $e) {
      error_log($e);
      $fbUser = null;
    }
}

if ($fbUser) {
    $logoutUrl = $facebook->getLogoutUrl(array( 'next' => ('my-url') ));
} else {
    $loginUrl = $facebook->getLoginUrl(array(
        'canvas' => 1,
        'fbconnect' => 0,
        'scope' => 'email,offline_access,publish_actions',
        'req_perms' => 'publish_stream,email',
        'redirect_uri' => 'my-redirect-url'
        ));
}

So I don't understand what is wrong.. When I test it, first time I can click on the loginUrl and get authorization . but when I try to getUser again, the function $facebook->getUser() return 0 again and I got the loginUrl again but when I click on the link, I'm just redirect on the callback url..

I'm not sure if the application facebook need special parameters for this. My app_id and app_secret are 100% sure.

回答1:

I solved this issue using
$facebook->getLoginUrl(array( 'scope' => 'public_profile', 'redirect_uri' => 'http://www.your_domain/current_page.php' ));
where 'http://www.your_domain/current_page.php' is the same page where i am running this code
N.B using Facebook php sdk 3.2.3



回答2:

(Question answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

OK I solved the error: the callback url was wrong, I needed to call the current url and not a special one.