Facebook authentication in function - getUser() returns 0

前端 未结 5 991
余生分开走
余生分开走 2021-01-18 18:33

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, eve

相关标签:
5条回答
  • 2021-01-18 19:07

    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(); }
      
    0 讨论(0)
  • 2021-01-18 19:07

    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.

    0 讨论(0)
  • 2021-01-18 19:12

    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)

    0 讨论(0)
  • 2021-01-18 19:13

    I figured out that the redirect-uri must instantiate the Facebook class and call getUser(), as there is some magic going on with the returned "code" from Facebook (as PCheese points out).

    So to clarify, it's no good redirecting to loggedin.html, you need to redirect to loggedin.php and call

    $facebook = new Facebook(array(
      'appId'  => 'yourAppID',
      'secret' => 'yourSecret',
    ));
    
    $user = $facebook->getUser();
    

    That will setup the proper session variables for your user.

    Also here is a page on Facebook explaining the whole process and how to exchange a "code" for an access token, if your not using thier php class, http://developers.facebook.com/docs/howtos/login/server-side-login/ (Step 6)

    0 讨论(0)
  • 2021-01-18 19:19

    I can't really explain why it works, but when I was experiencing problems with getUser() always returning a 0, I included the Javascript SDK in my page. Then it just worked. Maybe you can try if this will also solve your problem

    0 讨论(0)
提交回复
热议问题