当前位置:编程学习 > JS >>

JS端调用Facebook API

1. 申请Facebook账号

2.登陆http://developers.facebook.com/申请APPID

3.一定要对你申请的APP设置Site URL 和Site Domain, 如果你需要调试,可以设置为http://localhost/和localhost

4.在页面中引用JS, <script src="//connect.facebook.net/en_US/all.js"></script>

5.现在就可以调用API了,在调用API之前,必须要进行初始化

调用如下函数进行初始化:

[javascript] 
FB.init({ 
            appId: '447540288619439', // App ID 
            channelUrl: 'http://example.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 
            oauth : true 
        }); 

 

登陆:

[javascript] 
        function Test() { 
//            FB.api('/me', function (response) { 
//                alert('Your name is ' + response.name); 
            //            }); 
            FB.login(function (response) { 
                if (response.authResponse) { 
                    alert('Welcome!  Fetching your information.... '); 
                    FB.api('/me', function (response) { 
                        alert('Good to see you, ' + response.name + '.'); 
                    }); 
                } else { 
                    alert('User cancelled login or did not fully authorize.'); 
                } 
            }, { scope: 'email,user_location,offline_access,publish_stream' }); 
        } 

 

发布消息:要成功发布消息,必须让APP取得发送权限,注意上面登陆代码中的publish_stream

[javascript] 
function PostTest() { 
            var picurl = "http://102.mlsimages.movoto.com/064/12048664_0.jpg"; 
            var body = 'Hello every one, this is my house!'; 
            FB.api('/me/feed', 'post', { message: body, picture:picurl }, function (response) { 
                debugger; www.zzzyk.com
                if (!response || response.error) { 
                    alert('Error occured'); 
                } else { 
                    alert('Post ID: ' + response.id); 
                } 
            }); 
        } 
作者:diandian82

补充:web前端 , JavaScript ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,