在功能上Facebook验证 - 的getUser()返回0 [英] Facebook authentication in function - getUser() returns 0

查看:163
本文介绍了在功能上Facebook验证 - 的getUser()返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个函数在我的控制器张贴一张专辑到Facebook。每个功能被访问时,我重定向到REDIRECT_URI,仿佛$用户id = 0,即使我只是正常登录到Facebook上。这是因为在登录后,将重新启动功能,并创建一个新的Facebook会话,歼灭我刚刚得到了用户?

感谢您的帮助,您可以提供。

 函数份额(的$ id){
            如果(!$ ID){$这个 - >会话级> setFlash(__('专辑无效身份证',真));
            $这个 - >重定向(阵列('行动'=>'指数')); }
            $照片= $这个 - >&相册 - GT;找到('所有',阵列('条件'=>阵列('Album.id'=>的$ id)));
            $ Facebook的=新的Facebook(阵列(APPID=>'有效蚜','秘密'=>'有效秘密'));
            $ facebook-> setFileUploadSupport(真);
            $用户id = $ facebook->的getUser();            如果($用户id){
                尝试{
                      $ USER_PROFILE = $ facebook-> API('/我'); //获取基于用户已授予您的应用程序的权限的用户的信息。                }
                赶上(FacebookApiException $ E){
                    error_log中($ E);
                    //打印结果,如果要调试。
                    $用户ID = NULL;
                }            }其他{                $ LOGIN_URL = $ facebook-> getLoginUrl($ PARAMS =阵列('REDIRECT_URI'=> REDIRECT_URI,范围= GT; PERMISSIONS_REQUIRED));
                回声(<脚本> top.location.href ='。$ LOGIN_URL。'< / SCRIPT>中);
            }            //创建相册
            $结果= $ facebook-> API(ME /相册','后',$照片[0] ['相册'] ['名']); // - > photos_createAlbum($ ALBUMNAME,'', '','大家',$这个 - > _userid);        }


解决方案

默认情况下,Facebook类使用 PHP会话存储用户的认证状态。有可使用的四个会话变量


  • FB_ {} APPID _USER_ID :Facebook的用户ID

  • FB_ {} APPID _ code :授权code,需要一个访问令牌交换

  • FB_ {} APPID _access_token :可以用来做访问令牌API调用

  • FB_ {} APPID _STATE :CSRF令牌

检查你的PHP会话的配置和正常工作。


  • 默认情况下cookie的名称为PHPSESSID;你应该看到,在您的浏览器被设置。

  • 如果您是负载在多个服务器之间平衡你需要一个解决方案,让您在这些服务器共享会话状态。

  • 您不能输出启动会话之前的任何文本(自动,如果需要实例化的Facebook 对象时完成)。你可能会想尝试把这个作为你的函数的第一行:

     如果(!SESSION_ID()){在session_start(); }


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); 

        }

解决方案

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(); }
    

这篇关于在功能上Facebook验证 - 的getUser()返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆