如何续订一个Facebook user_access_token,如果我处理很多AJAX的?Facebook、user_access_token、AJAX

由网友(時光迫使我們成長)分享简介:请告诉我,如果我理解正确。 (因为我可能没有。)Please tell me if I'm understanding correctly. (because I might not be.)在用户的帖子我的网站上的东西。 (他看了也发布到Facebook。)在客户端发送一个AJAX POST请求发送到我的服务器,...

请告诉我,如果我理解正确。 (因为我可能没有。)

Please tell me if I'm understanding correctly. (because I might not be.)

在用户的帖子我的网站上的东西。 (他看了也发布到Facebook。) 在客户端发送一个AJAX POST请求发送到我的服务器,我的服务器插入记录在我的数据库中。 服务器实现了facebook的用户访问令牌已经过期,所以将响应发送回客户端,同时存储后的会话。 在客户端执行 window.location.replace(facebook_oauth_dialog_url) 然后,用户将看到一个突然闪,将Facebook的,然后回来的网站。我的服务器获取新的访问令牌。 在我的服务器检查会议,看看有什么应张贴到Facebook。然后,它采用了新的访问令牌张贴到Facebook。 User posts something on my site. (He checked "also post to Facebook".) Client sends an AJAX POST request to my server, and my server inserts the record in my database. The server realizes the the facebook user access token is expired, so it sends the response back to the client, while storing the post in a session. The client does a window.location.replace(facebook_oauth_dialog_url) Then the user will see a sudden "flash", going to Facebook, then coming back to the website. My server picks up the new access token. My server checks the session to see what should be posted to Facebook. And then, it uses the new access token to post that to Facebook.

真的是不是这样乏味?为什么我不能更新应用程序的服务器端无需用户去通过对话?

Is it really this tedious? Why can't I renew the app server-side without the user going through the dialog?

我的整个网站Backbone.js的。这意味着,这是一个大的页面。我不能跳了用户来回Facebook和我的网站是这样的。

My entire site is Backbone.js. That means, it's one big page. I can't jump the user back and forth between Facebook and my website like this.

推荐答案

我们的想法是利用的Facebook的JS -SDK 方法:

The idea is to make use of the Facebook JS-SDK methods:

用户选中的发布到Facebook 的选项 您检查当前用户连接到您的应用程序(使用 FB.getLoginStatus()) 如果用户连接,你有两个选择: 直接用 FB.api 方法,或交 将 access_token 到你的服务器来完成后期处理有 User check the Post To Facebook option you check if the current user is connected to your app (using FB.getLoginStatus()) if the user is connected, you have two options: post directly using the FB.api method or Send the access_token to your server to complete the post process there

下面是一个简单的例子(以已演示 !)为你开始:

Here's a quick example (with a Live Demo!) for you to get started:

<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<div id="fb-root"></div>
<script>
var fbLoaded = false;
window.fbAsyncInit = function() {
    FB.init({
      appId      : 'YOUR_APP_ID', // App ID
      //channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });
    fbLoaded = true;
    // Additional initialization code here

};

function postForm() {
    var msg = document.myForm.msg.value;
    // do form validation here, e.g:
    if(!msg.length) {
        alert("You should enter a message!");
        return false;
    }

    // do we need to post to Facebook?
    if(document.myForm.toFB.checked) {
        // is the library loaded?
        if(!fbLoaded) {
            alert("Facebook JS-SDK is not yet loaded. Please try again later or uncheck Post To Facebook option");
            return false;
        }

        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;
                /* 
                *  message can be posted to Facebook directly
                *  using the FB.api method or accessToken
                *  can be sent to the server and do the call
                *  from there
                */
                myAjaxCall(msg, accessToken);
            } else {
                // status is either not_authorized or unknown
                FB.login(function(response) {
                    if (response.authResponse) {
                        var accessToken = response.authResponse.accessToken;
                        myAjaxCall(msg, accessToken);
                    } else {
                        alert('User cancelled login or did not fully authorize.');
                    }
                }, {scope: 'publish_stream'});
            }
        });
    } else {
        myAjaxCall(msg);
    }
    return false;
}

function myAjaxCall(m,a) {
    alert("Here you make the ajax callnMessage: " + m + "nAccess Token: " + a);
}

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));
</script>

<form id="myForm" name="myForm" action="post" onSubmit="return postForm()">
<p><label>Your Message:</label><br/><textarea name="msg"></textarea></p>
<p><label>Post to Facebook?</label><input type="checkbox" value="1" name="toFB" /></p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
阅读全文

相关推荐

最新文章