如何续订一个Facebook user_access_token,如果我处理很多AJAX的? [英] How do I renew a Facebook user_access_token if I deal with a lot of AJAX?

查看:140
本文介绍了如何续订一个Facebook user_access_token,如果我处理很多AJAX的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 在用户的帖子我的网站上的东西。 (他看了也发布到Facebook。)
  2. 在客户端发送一个AJAX POST请求发送到我的服务器,我的服务器插入记录在我的数据库中。
  3. 服务器实现了facebook的用户访问令牌已经过期,所以将响应发送回客户端,同时存储后的会话。
  4. 在客户端执行 window.location.replace(facebook_oauth_dialog_url)
  5. 然后,用户将看到一个突然闪,将Facebook的,然后回来的网站。我的服务器获取新的访问令牌。
  6. 在我的服务器检查会议,看看有什么应张贴到Facebook。然后,它采用了新的访问令牌张贴到Facebook。
  1. User posts something on my site. (He checked "also post to Facebook".)
  2. Client sends an AJAX POST request to my server, and my server inserts the record in my database.
  3. 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.
  4. The client does a window.location.replace(facebook_oauth_dialog_url)
  5. 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.
  6. 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:

  1. 用户选中的发布到Facebook 的选项
  2. 您检查当前用户连接到您的应用程序(使用 FB.getLoginStatus()
  3. 如果用户连接,你有两个选择:
    • 直接用 FB.api 方法,或交
    • access_token 到你的服务器来完成后期处理有
  1. User check the Post To Facebook option
  2. you check if the current user is connected to your app (using FB.getLoginStatus())
  3. 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 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>

这篇关于如何续订一个Facebook user_access_token,如果我处理很多AJAX的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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