简体   繁体   English

Facebook Php SDK-getUser()始终返回0

[英]Facebook Php SDK - getUser() return always 0

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 . 我使用的是Facebook的最新Php SDK: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 . 所以我不明白这是怎么回事。当我测试它时,第一次我可以单击loginUrl并获得授权。 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.. 但是当我再次尝试getUser时,函数$facebook->getUser() return 0再次$facebook->getUser() return 0 ,并且再次获得了loginUrl,但是当我单击链接时,我只是重定向了回调URL。

I'm not sure if the application facebook need special parameters for this. 我不确定应用程序facebook是否为此需要特殊参数。 My app_id and app_secret are 100% sure. 我的app_id和app_secret是100%确定的。

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 其中'http://www.your_domain/current_page.php'与我运行此代码的页面相同
NB using Facebook php sdk 3.2.3 注意使用Facebook PHP SDK 3.2.3

(Question answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) ) (问题在评论中得到了回答。请参阅无答案的问题,但问题在评论中得到了解决(或在聊天中扩展了)

The OP wrote: OP写道:

OK I solved the error: the callback url was wrong, I needed to call the current url and not a special one. 好的,我解决了该错误:回调URL错误,我需要调用当前URL,而不是特殊的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM