Facebook-connect gives a redirect loop

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

Please, I need help. I'm dealing with this issue for 1 month!!

I would like to implement facebook connect login to my website, using PHP and php-sdk 3.1.1. In few words, my code works offline (on localhost) but not online which results in "Too many redirect loop (on Chrome)": Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

Here is my code:

1/ I load facebook connect SDK and init it:

    require 'src/facebook.php';     $facebook = new Facebook(array(         'appId'  => '209633612480053',         'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'     )); 

Please, note that I created two apps on facebook-developper page, one for offline tests, and the other for online tests. And I'm sure to switch correctly betwwen the two pairs of appId/secret (online and offline) when testing. So it's not the problem of bad facebbok-connect init.

2/ I try to get user info:

  $uid = $facebook->getUser();    if($uid)   {      /*       * Get user information.       */      $user = $facebook->api('me/');      print_r($user); // Display user info.   }   else   {      /*       * Redirect to FB login URL to allow access.       */      $loginURL = $facebook->getLoginURL();      echo '';   } 

It's as simple as that: If user ic connected to facebook, so display it's information, else, redirect to facebook login page to allow access.

IT WORKS PERFECTLY OFFLINE, but online, I get a chrome error:

This webpage has a redirect loop The webpage at https://www.facebook.com/dialog/oauth?client_id=209633612480053&redirect_uri=http%3A%2F%2Fwww.bluward.com%2Foauth%2Ffacebook&state=551f60cd4be6cd8ed1622f8168a5219a#_=_ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer. 

Some additional information: Online, I use 1and1 host provider, and to be sure to have the same server configuration than offline server (which is a MAMP Pro), I uploaded the sams php.ini file.

Please, if someone has an idea, or get a similar problem, I'll be glad to have help.

Thank you in advance for all help.

UPDATE:

I updated my code to focus on the problematic line, so instead of redirecting to facebook login page, I display the redirect URL, so I just have to click to login:

  $uid = $facebook->getUser();    if($uid)   {      /*       * Get user information.       */      $user = $facebook->api('me/');      print_r($user); // Display user info.   }   else   {      /*       * Redirect to FB login URL to allow access.       */      $loginURL = $facebook->getLoginURL();      echo $loginURL; // 

What I noticed is that facebook is infinitely redirecting to my script page. Only code parameter on URL bar changes.

So, why facebbok is redirecting to my script page without giving me user info?

Please, any idea?

回答1:

I had this problem, in my case I had to edit base_facebook.php from the SDK:

public static $CURL_OPTS = array(     CURLOPT_CONNECTTIMEOUT => 10,     CURLOPT_RETURNTRANSFER => true,     CURLOPT_TIMEOUT        => 60,     CURLOPT_USERAGENT      => 'facebook-php-3.2',     CURLOPT_SSL_VERIFYPEER => false,     CURLOPT_SSL_VERIFYHOST => false, ); 

The last two options I added manually.



回答2:

I had a similar issue with chrome browser, cookies always were incorrect or empty.

Then I downloaded a facebook example from Heroku (it uses a popup window as login instead of a js redirect) and noticed that it doesn't work in chrome when the base URL of the website isn't the server root URL (ex: localhost/somedirectory/). But after creating a virtual host it looks like it works.

You can see an example of the index.php from the application here https://gist.github.com/2845986



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!