Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Facebook JS SDK API - Post Image to Feed
function share_image(img_url, text) {
//Check to see if the user has authenticated the App.
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
//If you want the user's Facebook ID or their access token, this is how you get them.
var uid = response.authResponse.userID;
var access_token = response.authResponse.accessToken;
do_api_share(access_token, img_url, text);
} else {
//If they haven't, call the FB.login method
FB.login(function(response) {
if (response.authResponse) {
//If you want the user's Facebook ID or their access token, this is how you get them.
var uid = response.authResponse.userID;
var access_token = response.authResponse.accessToken;
do_api_share(access_token, img_url, text);
} else {
alert("You must install the application to share your greeting.");
}
}, {scope: 'publish_stream'});
}
});
}
function do_api_share(at, img_url, text) {
FB.api("/me/photos", 'post', { message: text, url: img_url}, function(response) {
console.log(response);
});
}
//Calling the function
share_img("http://example.com/images/image-one.jpg", "Hi there!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment