检查用户是否登录到facebook

时间:2012-07-10 07:38:37

标签: javascript facebook-graph-api

我在facebook上开发应用程序。如果他登录到facebook,他在阅读我网站上的任何文章时都可以发布到用户墙。一切正常,但我想做一些我希望我的应用程序检查用户是否登录到Facebook,如果不是应用程序必须强制他登录到Facebook才能阅读任何文章。

<html>
<head>
    <meta property="fb:app_id" content="452488354765730" />
    <meta property="og:title" content="3eesho reading club" />
    <meta property="og:type" content="website" />
    <meta property="og:url" content="http://3eesho.com/fb/subhitest/test_PAGE"  />
    <meta property="og:site_name" content="3eesho.com" />
    <meta property="og:image"       content="http://ogp.me/logo.png" /> 
</head>
    <body>
<script type="text/javascript">
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function checkCookie()
{
var a=document.location.href;
var b=a.split("/");
var username=getCookie("username");
if (username==null || username=="")
{
alert("thank you for visit this Id:"+b[4]);
username=b[4];
myFunction();
setCookie("username",username,365);
}
else 
{
alert("this Id visited:"+b[4]);
if(username!=b[4])
{
myFunction();
setCookie("username",b[4],365);
}   
}
}
function myFunction()
{
var x;
var r=confirm("did you want to publish this article to your wall on facebook");
if (r==true)
{
x=alert("the article published!");
graphStreamPublish();
}
else
{
x=alert("the article doesn't publish!");
}
}
</script>
<script>
            (function(d, s, id) {
                                            var js, fjs = d.getElementsByTagName(s)[0];
                                            if (d.getElementById(id)) return;
                                            js = d.createElement(s); js.id = id;
                                            js.src =   "//connect.facebook.net/en_US/all.js#xfbml=1&appId=452488354765730";
                                            fjs.parentNode.insertBefore(js, fjs);
                                            }(document, 'script', 'facebook-jssdk'));
//FB App Script
            var button;
            var userInfo;
            window.fbAsyncInit = function() {
                            FB.init({ appId: '452488354765730',
                            status: true,
                            cookie: true,
                            xfbml: true,
                            oauth: true});
                            showLoader(true);
            };
            (function() {
                            var e = document.createElement('script'); e.async = true;
                            e.src = document.location.protocol
                            + '//connect.facebook.net/en_US/all.js';
                            document.getElementById('fb-root').appendChild(e);
            }());
            //stream publish method
            function graphStreamPublish(){
                                alert("graphStreamPublish");
                            showLoader(true);
                            FB.api('/me/feed', 'post',
                            {
                             message :    "\u0642\u0631\u0627 \u0647\u0630\u0627 \u0627\u0644\u0645\u0642\u0627\u0644 \u0639\u0644\u0649 \u0639\u064A\u0634\u0648.\u0643\u0648\u0645",   
                                              link    : document.location.href+'? utm_campaign=spring&utm_medium=referral&utm_source=exampleblog'+target="_blank",
                                              },
                            function(response) {
                                            showLoader(false);
                                            if (!response || response.error) {
                                            //alert('Error occured');
                                            } 
                                                  else
    {

        alert("Posted as "+response.id);
    }
                            });
            }
            function showLoader(status){
                            if (status)
                                              document.getElementById('loader').style.display = 'block';
                            else
                                            document.getElementById('loader').style.display = 'none';
            }
</script>
<a href="#" onclick="checkCookie()">Testing</a>
<div class="fb-login-button" data-show-faces="true" data-width="180" data-max-rows="1"  scope="publish_actions"></div>
                <div id="loader" style="display:none"></div>
    </body>
</html>

0 个答案:

没有答案