我正在尝试创建一个Facebook iFrame应用。应用程序应首先显示图像,如果用户喜欢该页面,他将可以访问某些内容。
我使用RoR,因此我无法使用Facebook PhP SDK。
当用户不喜欢该页面时,这是我的iFrame HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<style type="text/css">
body {
width:520px;
margin:0; padding:0; border:0;
font-family: verdana;
background:url(repeat.png) repeat;
margin-bottom:10px;
}
p, h1 {width:450px; margin-left:50px; color:#FFF;}
p {font-size:11px;}
</style>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
</head>
<body>
<div id="container">
<img src="welcome.png" alt="Frontimg">
</div>
并且,如果用户喜欢该页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<style type="text/css">
body {
width:520px;
margin:0; padding:0; border:0;
font-family: verdana;
background:url(repeat.png) repeat;
margin-bottom:10px;
}
p, h1 {width:450px; margin-left:50px; color:#FFF;}
p {font-size:11px;}
</style>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
</head>
<body>
<div id="container">
<img src="member.png" alt="Frontimg">
<p>You liked this page</p>
答案 0 :(得分:80)
更新21/11/2012 @ALL:我已经更新了这个例子,以便更好地运用Chris Jacob和FB Best的帐户评论,看看工作示例here
嗨所以这里承诺的是我的回答只使用javascript:
页面的BODY内容:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : 'YOUR APP ID',
status : true,
cookie : true,
xfbml : true
});
</script>
<div id="container_notlike">
YOU DONT LIKE
</div>
<div id="container_like">
YOU LIKE
</div>
CSS:
body {
width:520px;
margin:0; padding:0; border:0;
font-family: verdana;
background:url(repeat.png) repeat;
margin-bottom:10px;
}
p, h1 {width:450px; margin-left:50px; color:#FFF;}
p {font-size:11px;}
#container_notlike, #container_like {
display:none
}
最后是javascript:
$(document).ready(function(){
FB.login(function(response) {
if (response.session) {
var user_id = response.session.uid;
var page_id = "40796308305"; //coca cola
var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
var the_query = FB.Data.query(fql_query);
the_query.wait(function(rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
$("#container_like").show();
//here you could also do some ajax and get the content for a "liker" instead of simply showing a hidden div in the page.
} else {
$("#container_notlike").show();
//and here you could get the content for a non liker in ajax...
}
});
} else {
// user is not logged in
}
});
});
那又做什么呢?
首先登录到FB(如果您已经拥有用户ID,并且您确定您的用户已经登录了Facebook,则可以绕过登录内容并将response.session.uid
替换为YOUR_USER_ID(来自您的rails应用程序)例如)
之后它在page_fan
表上进行FQL查询,意思是如果用户是页面的粉丝,则返回用户ID,否则返回一个空数组,然后返回根据结果,它显示div或其他。
此处还有一个工作演示:http://jsfiddle.net/dwarfy/X4bn6/
它使用可口可乐页面作为示例,试一试并喜欢/不像the coca cola page并再次运行...
最后一些相关的文档:
如果您有任何疑问,请不要犹豫。
干杯
更新2
正如有人所说,jQuery版本需要jQuery才能工作但是你可以轻松删除它(它只用于document.ready和show / hide)。
对于document.ready,你可以将你的代码包装在一个函数中并使用body onload="your_function"
或更复杂的东西,如:Javascript - How to detect if document has loaded (IE 7/Firefox 3),以便我们替换文档就绪。
对于展示和隐藏内容,您可以使用类似:document.getElementById("container_like").style.display = "none" or "block"
的内容以及更可靠的跨浏览器技术,请参阅此处:http://www.webmasterworld.com/forum91/441.htm
但是jQuery很容易:)
<强>更新强>
相对于我在这里发布的评论,这里有一些ruby代码来解码facebook POST到你的CANVAS URL的“signed_request”,当它取出它以便在facebook中显示时。
在你的动作控制器中:
decoded_request = Canvas.parse_signed_request(params[:signed_request])
然后是检查已解码的请求并显示一页或另一页的问题..(不确定这个,我对ruby感到不舒服)
decoded_request['page']['liked']
这是相关的Canvas类(来自fbgraph ruby library):
class Canvas
class << self
def parse_signed_request(secret_id,request)
encoded_sig, payload = request.split('.', 2)
sig = ""
urldecode64(encoded_sig).each_byte { |b|
sig << "%02x" % b
}
data = JSON.parse(urldecode64(payload))
if data['algorithm'].to_s.upcase != 'HMAC-SHA256'
raise "Bad signature algorithm: %s" % data['algorithm']
end
expected_sig = OpenSSL::HMAC.hexdigest('sha256', secret_id, payload)
if expected_sig != sig
raise "Bad signature"
end
data
end
private
def urldecode64(str)
encoded_str = str.gsub('-','+').gsub('_','/')
encoded_str += '=' while !(encoded_str.size % 4).zero?
Base64.decode64(encoded_str)
end
end
end
答案 1 :(得分:19)
您需要编写一些PHP代码。当用户首次单击选项卡时,您可以检查他是否喜欢该页面。以下是示例代码
include_once("facebook.php");
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true,
));
$signed_request = $facebook->getSignedRequest();
// Return you the Page like status
$like_status = $signed_request["page"]["liked"];
if($like_status)
{
echo 'User Liked the page';
// Place some content you wanna show to user
}else{
echo 'User do not liked the page';
// Place some content that encourage user to like the page
}
答案 2 :(得分:5)
这是一个工作示例,它是this answer:
的分支$(document).ready(function(){
FB.login(function(response) {
if (response.status == 'connected') {
var user_id = response.authResponse.userID;
var page_id = "40796308305"; // coca cola page https://www.facebook.com/cocacola
var fql_query = "SELECT uid FROM page_fan WHERE page_id="+page_id+" and uid="+user_id;
FB.api({
method: 'fql.query',
query: fql_query
},
function(response){
if (response[0]) {
$("#container_like").show();
} else {
$("#container_notlike").show();
}
}
);
} else {
// user is not logged in
}
});
});
我使用了FB.api方法(JavaScript SDK),而不是不推荐使用的FB.Data.query。 或者您可以像使用此示例一样使用Graph API:
$(document).ready(function() {
FB.login(function(response) {
if (response.status == 'connected') {
var user_id = response.authResponse.userID;
var page_id = "40796308305"; // coca cola page https://www.facebook.com/cocacola
var fql_query = "SELECT uid FROM page_fan WHERE page_id=" + page_id + " and uid=" + user_id;
FB.api('/me/likes/'+page_id, function(response) {
if (response.data[0]) {
$("#container_like").show();
} else {
$("#container_notlike").show();
}
});
} else {
// user is not logged in
}
});
});
答案 3 :(得分:2)
JavaScript代码需要进行一些更改才能根据用户的喜好来处理渲染,或者不喜欢Facebook强制要求的页面转移到Auth2.0授权。
变化相当简单: -
会话必须由authResponse替换,uid由userID替换
此外,考虑到代码的要求以及人们(包括我)在FB.login中所面临的一些问题,使用FB.getLoginStatus是一个更好的选择。如果用户已登录并已对您的应用进行身份验证,它会将查询保存到FB。
有关如何将查询保存到FB服务器的信息,请参阅响应和会话对象部分。 http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
使用FB.getLoginStatus解决FB.login及其修复问题。 http://forum.developers.facebook.net/viewtopic.php?id=70634
以上是上面发布的代码,其中包含适用于我的更改。
$(document).ready(function(){
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
var user_id = response.authResponse.userID;
var page_id = "40796308305"; //coca cola
var fql_query = "SELECT uid FROM page_fan WHERE page_id =" + page_id + " and uid=" + user_id;
var the_query = FB.Data.query(fql_query);
the_query.wait(function(rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
$("#container_like").show();
//here you could also do some ajax and get the content for a "liker" instead of simply showing a hidden div in the page.
} else {
$("#container_notlike").show();
//and here you could get the content for a non liker in ajax...
}
});
} else {
// user is not logged in
}
});
});
答案 4 :(得分:1)
使用Javascript SDK,您可以更改下面的代码,这应该在FB.init调用后添加。
// Additional initialization code such as adding Event Listeners goes here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
alert('we are fine');
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
alert('please like us');
$("#container_notlike").show();
} else {
// the user isn't logged in to Facebook.
alert('please login');
}
});
FB.Event.subscribe('edge.create',
function(response) {
alert('You liked the URL: ' + response);
$("#container_like").show();
}
答案 5 :(得分:0)
这里有一篇描述你的问题的文章
http://www.hyperarts.com/blog/facebook-fan-pages-content-for-fans-only-static-fbml/
<fb:visible-to-connection>
Fans will see this content.
<fb:else>
Non-fans will see this content.
</fb:else>
</fb:visible-to-connection>