登录Facebook API后如何保持Twitter API会话? [英] How to keep Twitter API session alive after logging through Facebook API?

查看:137
本文介绍了登录Facebook API后如何保持Twitter API会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Facebook,Google+和Twitter两个不同的API代码位于三个不同的文件夹中:Facebook API的Facebook文件夹,Twitter的twitter文件夹和google_plus。

The two different API code for Facebook, Google+ and Twitter is located in three respective different folders: "facebook" folder for the Facebook API, "twitter" folder for Twitter and "google_plus".

我试图允许人们通过Twitter登录并访问应用程序,并通过Twitter登录后通过Facebook登录,并在他们登录后保留基于twitter的数据通过Facebook。我基本上希望他们能够同时查看他们的Twitter和Facebook Feed以及其他数据混合在一起。

I'm trying to allow people to log on through Twitter and access the application, and give them the option to also log on through Facebook after logging on through Twitter and keep the twitter-based data after they logged through Facebook. I would basically like them to be able to view both their Twitter and Facebook feeds and other data mixed together at the same time.

我尝试实现会话,但它看起来就像在通过Twitter登录后通过Facebook登录,Twitter会话变量内容变为NULL,只显示Facebook会话数据。如何操作会话,以便用户通过Facebook登录时可以保留Twitter会话数据?

I tried to implement sessions, but it looks like if they log through Facebook after logging through Twitter first, the Twitter session variable content become NULL and only Facebook session data is displayed. How can I manipulate the sessions so that Twitter session data can be kept when a user log through Facebook as well?

以下是我对数据进行测试: p>

Below is what I did to test the data:

session_start();
require('../database/connection.php');
require_once('../twitter/twitteroauth/twitteroauth.php');
require_once('../twitter/config.php');
include_once '../facebook/fbmain.php';

这里我将测试$ _SESSION变量,以查看用户登录后是否保留twitter数据Facebook也是..但是Twitter数据变为NULL,而$ _SESSION数组只显示Facebook数据。在某人登录Facebook之后,如何保持Twitter会话数据?

Here I'll test the $_SESSION variable to see if twitter data is kept after a user logs through Facebook as well .. but Twitter data become NULL instead and the $_SESSION array only show Facebook data .. How can I make Twitter session data STAY after someone logs through Facebook as well ?

var_dump($_SESSION); var_dump(session_id());  // twitter session data gets overriden by facebook data although the variable names are different ..
/* If access tokens are not available redirect to connect page. */

if (empty($_SESSION['facebook_id']) && (empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret']))) {
header('Location: ./clearsessions.php');
}
else {
/* Get user access tokens out of the session. */
$access_token = $_SESSION['access_token'];


/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

/* If method is set change API call made. Test is called by default. */
$content = $connection->get('account/verify_credentials');
$json = json_encode($content);
$data = json_decode($json,true);
$screen_name = $data["screen_name"];
$name = $data["name"];
$image_url = $data['profile_image_url'];
$_SESSION['screen_name'] = $screen_name;
$_SESSION['image_url'] = $image_url;
$query = "SELECT * FROM Users WHERE username ='$screen_name'";
$result = mysql_query($query);
$result_count = mysql_num_rows($result); 
if($result_count == 0) {
$insert = "INSERT INTO Users(username) VALUES('$screen_name')";
$result_insert = mysql_query($insert);
}

}

if(isset($_SESSION['oauth_token']) && isset($_SESSION['oauth_token_secret']) && isset($_SESSION['facebook_id'])){

$user = $_SESSION['facebook_id'];

}



?>


推荐答案

调试他的应用程序后,重定向到他的 www 子域名,而Twitter被设置为重定向到没有子域。自从cookie被严格发布以来,会话丢失了。

After debugging his application it turned out his Facebook was setup to redirect to his www subdomain, while Twitter was setup to redirect to no subdomain. The session was lost since the cookie was issued on a strict basis.

所以在通过Facebook登录后,用户被带到 www 发出一个会话/ cookie,通过Twitter登录后,用户被带到纯域,发送/呈现完全不同的会话/ cookie。

So after logging in via Facebook the user was taken to www which issued one session/cookie, after logging via Twitter the user was taken to plain domain, where a completely different session/cookie was being issued/presented.

解决问题的方法:


  1. 通配符Cookie session_set_cookie_params(0,'/','.mywebsite.com' );

  2. 更好的解决方案 - 不要混合和匹配子域名,设置一个规范的URL并坚持下去。

这篇关于登录Facebook API后如何保持Twitter API会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆