简体   繁体   English

PHP-SDK Facebook身份验证不断重定向

[英]PHP-SDK Facebook auth keeps redirecting

I am trying to integrate Facebook into a web app that I am working on. 我正在尝试将Facebook集成到我正在开发的Web应用程序中。 I got an api key and secret key from Facebook, and have set the Site Url to http://www.mysite.com/something/app/ 我从Facebook获得了api密钥和秘密密钥,并将站点网址设置为http://www.mysite.com/something/app/

The following code is used from http://www.mysite.com/something/app/signup/xxxx/ to detect whether the user is connected or not: 来自http://www.mysite.com/something/app/signup/xxxx/的以下代码用于检测用户是否已连接:

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

        $session = $facebook->getUser();

        if($session) {
            # Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
            try{
                $uid = $facebook->getUser();
                $user = $facebook->api('/me');
            } catch (Exception $e){}

            if(!empty($user)){
                # User info ok? Let's print it (Here we will be adding the login and registering routines)
                echo $user;
            } else {
                # For testing purposes, if there was an error, let's kill the script
                die("There was an error.");
            }
        } else {
            # There's no active session, let's generate one
            $login_url = $facebook->getLoginUrl();
            header("Location: ".$login_url);
        }

But the problem is, when the user is redirected to $login_url, it keeps redirecting back, creating an infinite loop. 但是问题是,当用户被重定向到$ login_url时,它将不断地向后重定向,从而形成无限循环。

The redirect url is: https://www.facebook.com/connect/uiserver.php?app_id=xxxxxxxxxxx&method=permissions.request&display=page&next=http%3A%2F%2Fwww.something.com%2Fsomething%2Fapp%2Fsignup%2Fxxxxx%2F&response_type=code&state=6f51768c22974a16b5dcf4d8dc7f46df&fbconnect=1 重定向网址为: https : //www.facebook.com/connect/uiserver.php?app_id=xxxxxxxxxxx&method=permissions.request&display=page& next=http% 3A% 2F%2Fwww.something.com%2Fsomething%2Fapp%2Fsignup% 2Fxxxxx %2F&RESPONSE_TYPE =代码&状态= 6f51768c22974a16b5dcf4d8dc7f46df&fbconnect = 1

I have looked at other similar issues on SO, and also on Google and have tried their solutions but it just didn't help me out. 我在SO上以及Google上都看过其他类似的问题,并尝试过他们的解决方案,但这并没有帮助我。

[UPDATE] [UPDATE]

Ok I think I am onto something. 好吧,我想我正在做某事。 If I log in as a different Facebook user, I am not being redirected in a loop. 如果我以其他Facebook用户身份登录,则不会被循环重定向。 It seems to happen only when I use my own Facebook account, which is the admin of the app. 似乎只有当我使用自己的Facebook帐户(该应用程序的管理员)时才会发生。 But after the redirect it should return getUser() right.. but it doesn't for some reason. 但是在重定向之后,它应该正确返回getUser()。但是由于某种原因,它没有。

Any ideas? 有任何想法吗? Thanks. 谢谢。

I started from scratch on a new .php file from the root of my server. 我从服务器的根目录开始从一个新的.php文件开始。 There everything was working alright, so I found out that my own code was causing the webpage to redirect. 一切正常,所以我发现自己的代码导致网页重定向。

In fact, it was the mod_rewrite in my .htaccess file that as messing things up. 实际上,正是我的.htaccess文件中的mod_rewrite弄得一团糟。 Somehow it is not picking up the GET variables set by FB. 不知何故,它没有选择FB设置的GET变量。 So I need to figure out something for it. 所以我需要弄清楚一些东西。 But at least I now know what caused the problem. 但是至少我现在知道是什么原因引起的。 Case closed! 结案!

I have just had the same problem. 我刚遇到同样的问题。 When I tried to use the same code (the latest FB SDK) that is working on another server in the current server, the infinite redirection loop occurred. 当我尝试使用与当前服务器中的另一台服务器相同的代码(最新的FB SDK)时,发生了无限重定向循环。

When I started debugging it, I found out that the reason was because that the current server $_REQUEST does not seem to process the value in $_GET . 当我开始调试它时,我发现原因是因为当前服务器$_REQUEST似乎$_REQUEST $_GET中的值。 Thus, both the $_GET['state'] and $_GET['code'] do not get read in the $_REQUEST . 因此, $_GET['state']$_GET['code']都不会在$_REQUEST读取。 In base_facebook.php , it seems that they are using $_REQUEST instead of $_GET to get the code and state value. base_facebook.php ,似乎他们使用$_REQUEST而不是$_GET来获取代码和状态值。

Thus, the easy fix for this is to include the following code: 因此,对此的简单解决方法是包括以下代码:

$_REQUEST['state'] = $_GET['state'];
$_REQUEST['code'] = $_GET['code'];

Before the require_once of facebook SDK file. 在facebook SDK文件的require_once之前。

Hope this will help some people out there! 希望这会帮助一些人!

I'm having the same issue here. 我在这里遇到同样的问题。 Here is a simple code, it runs on a single site and is as simple as it can be: 这是一个简单的代码,它在单个站点上运行,并且非常简单:

@ini_set('log_errors','On'); // enable or disable php error logging (use 'On' or 'Off')
@ini_set('display_errors','On'); // enable or disable public display of errors (use 'On' or 'Off')
include_once('lib/facebook.php');

// Create our application instance
$config = array(
  'appId' => 'xx',
  'secret' => 'yyy'
);
$link = 'http://someurl.com';
$facebook = new Facebook($config);

// Get User ID
$user = $facebook->getUser();

if($user){ ?>
    <p>logged in</p>
<?php } else {
    $loginUrl = $facebook->getLoginUrl(array(
    'scope'         => 'manage_pages,email',
    'redirect_uri'  => $link,
    ));
    echo '<a href="'.$loginUrl.'">Login with FB</a>'; 
}

So what id does, it showes link to login when user is not logged in (or to be specific - if getUser() doesnt recive anything), and paragraph with text if getUser gives what it should give. 所以id会做什么,它会显示用户未登录时登录的链接(或者是特定的-如果getUser()无法接收任何内容),以及带有文本的段落(如果getUser给出了应提供的内容)。

Simple test will show a problem. 简单的测试将显示一个问题。 Accessing it by site url, even when user permission is given, shows login url instead of paragraph. 即使授予用户权限,通过站点url访问它也会显示登录URL而不是段落。

Where is the problem? 问题出在哪儿? While trying to figure it out, I have added site's url as "App on Facebook", so it can be accessed from inside of facebook. 在尝试弄清楚时,我将网站的网址添加为“ Facebook上的应用”,以便可以从Facebook内部访问它。 And eureka, if it is accessed this way (by Canvas Page url), getUser() works correctly. 还有eureka,如果通过这种方式(通过Canvas Page url)进行访问,则getUser()可以正常工作。 Whats even more, when once You have visited through Canvas Page url link, then when visiting site directly - getUser() also works correctly. 更重要的是,一旦您通过“画布页面” URL链接进行了访问,然后直接访问了站点,则getUser()也可以正常工作。

So where is the catch? 那么渔获量在哪里? Trying with browser's incognito function i have figured otu, that there is something stored on user browser. 尝试使用浏览器的隐身功能时,我已经想到otu,用户浏览器中存储了一些内容。 Visiting site directly from a browser, where user already have been on Canvas Page url shows correct responce, while when on a browser where user havent visited Canvas Page url, shows login url. 直接从浏览器访问网站(用户已经在“画布页面” URL上)显示正确的响应,而在浏览器上用户没有访问“画布页面” URL时,显示登录URL。

So it seams, that the problem is somehow connected with browser cache, and facebook doesnt set it correct way when visiting throught page instead of Canvas Page itself. 因此,它暗示了问题是与浏览器缓存有关,Facebook在访问整个页面而不是Canvas页面本身时未设置正确的方式。

Anybody have an idea how to correct this issue working only with php sdk? 有人有一个想法如何纠正仅与php sdk一起使用的问题吗?

Edit Also, when acces to app is given, when user clicks on login link, redirect gets back to oryginal page with state and code as _GET parameters. 编辑也,当给应用程序访问权限时,当用户单击登录链接时,重定向将以状态和代码作为_GET参数返回到原始页面。

It looks like you are using an old version of the PHP SDK with code that is meant to work with the latest version. 您似乎正在使用旧版本的PHP SDK,并且该代码应与最新版本一起使用。 Upgrade the PHP SDK to the newest version - v3.1.1 将PHP SDK升级到最新版本-v3.1.1

https://github.com/facebook/php-sdk https://github.com/facebook/php-sdk

Then that code should work. 然后该代码应该起作用。


Have you tried specifying a redirect uri: 您是否尝试过指定重定向uri:

<?php
    $loginUrl = $facebook->getLoginUrl(
        array( 'redirect_uri' => 'http://www.mysite.com/something/app/' )
    );
?>

The script behind the target url would need to create a facebook object instance for the users session to persist. 目标网址后面的脚本需要创建一个Facebook对象实例,以使用户会话持久化。

I've had this issue on certain browsers, and ended up being the browsers fault by blocking the cookie set by the site, and therefore the session. 我在某些浏览器上遇到了这个问题,最终由于阻止站点设置的cookie以及会话而导致了浏览器故障。

Try adding this to your page: 尝试将其添加到您的页面:

header('P3P: CP="CAO PSA OUR"');

(Its PHP, you can also do it on a meta tag via HTML) (它是PHP,您也可以通过HTML在meta标记上执行此操作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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