简体   繁体   中英

Logging in with Facebook PHP SDK works at home, but not at work (getUser() returns 0)

This is the first time I've tried working with Facebook. I spent a lot of my time at work a couple days ago, trying to get it all to work. It looked like everything was in order according to all of the examples in the Facebook documentation, but it just wouldn't work. The files are hosted on a site, so I'm not trying to work from localhost or anything like that, I have the app's site settings correct on facebook.

When I went home that night, I decided to work on it a little bit more and when I fired up the site, it worked exactly like it was supposed to. I figured it must have taken awhile to get the app settings to take affect and whatnot and just continued working on it all night. Made a bunch of progress and called it a night.

Next day at work. I open up my web browser, head out to the site to check out my awesome progress again, doesn't work. Remote desktop into home computers, works fine. Worked fine in IE, firefox, and chrome on my wife's windows computer, and worked fine in firefox and chrome on my linux desktop. At work, it doesn't work on IE, firefox, or chrome. None of this should matter because I'm not doing any of it on client side.

I've tried deauthorizing the app and it sends me to the authorize page, where I accept, but then it just returns 0 as the user. I can't figure out why this wouldn't just work the same, it's all server side and I'm just trying to echo the user at this point.

EDIT: Also, deauthorizing and reauthorizing works fine at home.

EDIT2: Here is the simple code.

<?php
require 'facebook.php';  //sdk
$facebook = new Facebook(array(
  'appId' => 'yep',
  'secret' => 'yep'
));

try {
    $uid = $facebook->getUser();
    $fbme = $facebook->api('/me');
} catch (FacebookApiException $e) { 
    print_r($e);
}
?>

It results in this error message, even after deauthorizing and reauthorizing the app and permissions. "An active access token must be used to query information about the current user."

Only thing I can imagine is some sort of firewall, access gate or a browser security setting that restricts the usage of cookies (session cookies) to be sent over your network at the office.

Facebook SDK makes use of sessions, so when your browser at work doesn't send the session cookies or the network filters them... there's your problem.

So, it's working now. But I don't know why it wasn't.

This is what I did:

  1. Deauthorized app
  2. cleared cookies and cache
  3. restarted computer (I went all out)
  4. came back to the site
  5. reauthorized app

it works.

Only thing I hadn't tried before was #2.

EDIT: No longer working at work or at home. I deauthorized the app and after reauthorizing and reaccepting the permissions, I would assume it would give me a fresh, shiny, new access token but I keep getting the same error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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