如果我处理大量的AJAX,如何更新Facebook user_access_token?

TIM*_*MEX 7 javascript ajax facebook facebook-graph-api

如果我理解正确,请告诉我.(因为我可能不会.)

  1. 用户在我的网站上发布内容.(他检查了"也发布到Facebook".)
  2. 客户端向我的服务器发送AJAX POST请求,我的服务器在我的数据库中插入记录.
  3. 服务器意识到facebook用户访问令牌已过期,因此它将响应发送回客户端,同时将帖子存储在会话中.
  4. 客户做了一个 window.location.replace(facebook_oauth_dialog_url)
  5. 然后用户将看到突然的"闪光",进入Facebook,然后回到网站.我的服务器获取新的访问令牌.
  6. 我的服务器检查会话以查看应该发布到Facebook的内容.然后,它使用新的访问令牌将其发布到Facebook.

这真的很乏味吗?如果没有用户通过对话框,为什么我不能更新应用服务器端?

我的整个网站是Backbone.js.这意味着,这是一个重要的页面.我不能像这样在Facebook和我的网站之间来回跳转用户.

ifa*_*our 8

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

  1. 用户检查Post To Facebook选项
  2. 检查当前用户是否已连接到您的应用(使用FB.getLoginStatus())
  3. 如果用户已连接,您有两种选择:
    • 直接使用FB.api方法或发布
    • 发送access_token到您的服务器以完成那里的后期处理
  4. 如果用户未连接(或未登录到Facebook),请使用该FB.login()方法

这是一个快速示例(使用现场演示!)供您开始使用:

<!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 call\nMessage: " + 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>
Run Code Online (Sandbox Code Playgroud)

  • 完美答案.我如何给你额外的赏金? (2认同)