Facebook API does not find logged in user

Asked

Viewed 218 times

4

I minimized my code as little as possible in order to isolate the error. I’ve reviewed the forum looking for a simple answer, but no hint solved this basic problem.

In the app settings, the only changes I made were filling in the url (localhost:81/Mb - I’ve already published the file and changed the domain and the result was the same) and set the Default Activity Privacy clause as Public in the App Center Permissions. I need to do something else?

This is a PHP page:

include_once 'libs/facebook/src/facebook.php';

$facebook = new Facebook(array(
    'appId'  => '###',
    'secret' => '###',
    'cookie' => true
));

$user = $facebook->getUser();

$login_url = $facebook->getLoginUrl();
echo 'Please <a href="' . $login_url . '">login.</a>';

if($user == null) {
    print_r("user null " . $user);
} else {
    print_r("user logged" . $user);
}

Although you are logged in to facebook and use another Chrome tab the result is "user null 0".

  • You gave permission to your application with the user account?

  • When do these permissions need to be given? I set up site dominance in the facebook developer session.

  • When you try to enter the site, It is to automatically divert to facebook, Profile allow access to the app information. Also, which URL did you put in for the app? If you’re testing locally, you need to put in localhost. Also, where do you have it ### are the ID and the secret of the app right? otherwise, not even chance to funfar.

  • Felipe, yes to everything you mentioned: I’m testing locally and the saved URL is localhost and the appid and Secret codes are properly copied from the same page of the app on Facebook. And yet, user = null.

  • 1

    Probably the user is not logged in to Facebook when accessing your page. In the SDK for Javascript (with which I have some experience), you can request the login, but in the SDK for PHP I honestly do not know. Looking at this official example, it looks like you need to provide a link to the login: https://developers.facebook.com/docs/php/howto/profilewithgraphapi/

  • That’s exactly it, you shouldn’t be ordering the login. When the $user is equal to null, you should use: $login_url = $facebook->getLoginUrl();&#xA; echo 'Please <a href="' . $login_url . '">login.</a>';

  • Luiz, Felipe - although the user is already logged in, after all I was using Facebook in the neighbor tab (maybe I’m not understanding the concept of logged in), I created HREF to log in. When I click on "login", the URL changes "? code=XXXXX", but nothing happens. F5 does not change anything...

  • So, man, what happens when you click the login is you redirect yourself to facebook and from there, you give the permissions to the app. Like when you’re going to access a new game, which says the app can post things on your wall instead, etc...

  • This is exactly what I wanted to happen... But this URL only appears: http://localhost:81/Mb/post_stalker.php? code=aqcpb3nxplwomhbyluci2hq-gzuSO9OqJ3R_Y2WU1IVSPWLuvPbRT9G3MOy3V6sPBGBmnx8zTryXM1-Figcw2lsv276vaiwfim2whszxfcesjjhywnzag4rmijpruropyxgo5ap3rhf6ntrldzp57j08zrcq_6huwz9mio5om8tuy2hsutbcififqso2rkazdwk2j7lqqb_0hx3ffizacgjjstjtjjjjgeydidm7wpvz9xd-wAO6ZXeHnQJRqJ7cSoufAdHaXxzkpoQ1JEpyFXc2rhmbrHqsk-roB78gMNYYUeTiC3pjiYD887-JmLY&state=b1385f1e529f520c3ca8733e00ed2141#=

  • Ah, got it, the permissions page isn’t showing up, is it? IT SEEMS TO BE PROBLEM with the app settings.

  • In the app settings, the only changes I made were filling in the url (http://localhost:81/Mb) and setting in App Center Permissions the Default Activity Privacy clause as Public. I need to do something more?

  • The ideal is to put as much information as possible editing the question itself to make it easier for everyone to understand the problem. If the new information is only in the comments, the chances of getting a reply decrease.

  • Does it have to be door 81? Maybe that’s why...

  • I thought that too. I already uploaded to the domain and continued with the same problem.

  • Add to the 'status' => true' initialization array

  • 4

    Puts what you edited in a reply explaining how you did to solve the problem.

  • Thanks for the tip! I had already edited in the question itself and now I created an answer!

Show 12 more comments

1 answer

1


The problem was in the configuration of the app on Facebook. To solve it I had to adjust this option:

Do you want do make this app and all its live Features available to the general public?

in the Status & Reviews tab.

  • You can accept your own answer. There on the left, below the votes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.