Skip to content

Instantly share code, notes, and snippets.

@nishinoshake
Last active May 29, 2016 05:19
Show Gist options
  • Save nishinoshake/4e0569fe3fe87f900620 to your computer and use it in GitHub Desktop.
Save nishinoshake/4e0569fe3fe87f900620 to your computer and use it in GitHub Desktop.
Facebookフィード取得(JSのみ) | トークンだだ漏れ
getUserFeed(function(feed) {
for ( var i = 0; i < feed.length; i++ ) {
var obj = feed[i];
var text = restrictString(obj.text, 20);
var html = '<section class="fb-feed">';
html += '<a href="' + obj.link + '" target="_blank">';
if ( obj.src ) html += '<p class="fb-thumb"><img src="' + obj.src + '"></p>';
html += '<p class="fb-text">' + text + '</p>';
html += '<p class="fb-date">' + obj.date + '</p>';
html += '</a>';
html += '</section>';
// $('#feed-area').append(html);
}
});
function getUserFeed(callback) {
window.fbAsyncInit = function() {
FB.init({
appId : '',
xfbml : true,
version : 'v2.4'
});
FB.api(
// userid/feed
'/xxxx/feed',
{
//param: access_token -> app access tocen
//param: fields -> https://developers.facebook.com/docs/graph-api/reference/v2.4/post
access_token: 'xxxx|yyyy',
fields : 'id,status_type,object_id,link,full_picture,message,created_time'
},
//callback: response -> json
function(response) {
if ( ! response || response.error ) return false;
var feed = [];
var count = 0;
for (var i = 0; i < response.data.length; i++) {
var obj = response.data[i];
if ( obj.status_type === 'added_photos' || obj.status_type === 'shared_story' ) {
var userId = obj.id.match(/^(\S*)_/)[1];
var postId = obj.id.match(/_(\S*)$/)[1];
feed[count] = {};
feed[count].id = obj.id;
feed[count].objectId = obj.object_id ? obj.object_id : '';
feed[count].link = 'https://www.facebook.com/' + userId + '/posts/' + postId;
feed[count].text = obj.message ? obj.message : '';
feed[count].date = obj.created_time ? obj.created_time.match(/^(\S+)T/)[1].replace(/\-/g, '.') : '';
feed[count].src = obj.object_id ? 'https://graph.facebook.com/' + obj.object_id + '/picture?width=480' : obj.full_picture ? obj.full_picture : '';
count++;
}
}
callback(feed);
}
);
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment