☆ 最近發現 GitHub 實在太強大了,真的是一個能取代 Blog 的服務,未來大概只會更新到 GitHub 上。
https://github.com/kelp404

2012-11-26

Post a photo to Facebook with Facebook JavaScript SDK

First, you should include Facebook JavaScript SDK into your web.
<script type="text/javascript" src="http://connect.facebook.net/en/all.js"></script>

Call FB.init() when $(document).ready().
$(document).ready(function () {
    // set up
    FB.init({ appId: 'Your Facebook App ID' });
});

Post a photo and publish into stream
// Facebook login
FB.login(function(response) {
    if (response.authResponse) {
        // login success, then post a photo
        FB.api('/albumID/photos',
               'post',
               {
                   message: 'Your Message',
                   url: 'http://passbook.accupass.com/imgs/photo.png'
               },
               function(response) {
                 console.log(response);
                 if (!response || response.error) {
                     console.log('failed');
                 } else {
                     console.log('success');
                 }
               }
        );
    }
}, {scope: 'publish_stream'});

Facebook logging in with scope "publish_stream" let the photo could be publish into the stream.


Reference:

1 comment: