如何使用Facebook JavaScript SDK生成访问令牌

时间:2014-02-28 23:28:23

标签: javascript php facebook facebook-graph-api facebook-javascript-sdk

我正在使用JavaScript SDK创建一个应用程序,列出参加公共活动的用户。当用户登录时,一切都很好,但是在尝试查看数据时没有登录就会出错(因为没有访问令牌)。

如何在不登录的情况下安全地生成访问令牌以显示事件详细信息?我知道它可以在PHP SDK的服务器端完成,但我宁愿坚持使用JavaScript,除非我别无选择。感谢您的回复!

window.fbAsyncInit = function() {
FB.init({
    appId      : '{my-app-id}',
    status     : true,
    xfbml      : false
});

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;

        setupApp();
        alert('Connected');

    } else if (response.status === 'not_authorized') {
        // the user is logged in to Facebook, 
        // but has not authenticated your app

        alert('Not authenticated.');

    } else {
        // the user isn't logged in to Facebook.

        alert('Not logged in.');

    }
}); // end getLoginStatus


function FBLogin() {
    //console.log('login');
    FB.login(function (response) {
        if (response.authResponse) {
            alert('wooooohoo');
        }
    }, {
        scope: 'rsvp_event,publish_stream,offline_access,friends_events'
    });
}}; // end Init

0 个答案:

没有答案