I have been using Graph API along with javascript facebook sdk to make posts on users wall. The code looks like this
function graphStreamPublish(){
showLoader(true);
FB.api('/me/feed', 'post',
{
message : "Sample Message",
link : 'link url',
picture : 'image url',
name : 'app name',
description : 'Test stuff'
},
function(response) {
showLoader(false);
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}
Can this piece of code be used to post on users timeline as well or is there something else I need to do?
The following code works. ( Notice the 'publish_actions' permission used at the end).
FB.login(function(response) {
// handle the response
if (response.status === 'connected') {// Logged into your app and Facebook.
FB.api('/me/feed', 'post',
{
message : "Sample Message",
name : 'app name',
description : 'Test stuff'
},
function(response) {
if (!response || response.error) {
alert(response.error);
} else {
alert('Post ID: ' + response.id);
}
});
}
}, {scope: 'public_profile,email,publish_actions'} );
Years ago, you could use field TO with user id, nowadays you cant post to users timeline, but you can use TO field with an ID, for posting on groups, events or pages .
Related
I want to post into a facebook page with image. I got this error (#200) Subject does not have permission to post photos on this page. Please help me. I don't know what to do.
FB.login(function(auth) {
if (auth.status === 'connected') {
FB.api('/me/accounts', 'get', function(response) {
response.data.forEach(function(item) {
if (item.id == "<page_id>") {
post_to_page(item.access_token);
}
});
});
}
}, {scope: 'manage_pages, publish_pages, publish_actions'});
function post_to_page(access_token_data) {
var data = {
url: 'https://i.imgur.com/iXEkIJf.jpg'
}
FB.api('/<page_id>/photos', 'post', data, function(response) {
if (!response || response.error) {
console.log(response.error);
} else {
console.log(response.id);
}
});
}
I got it already and it works!
var FACEBOOK = {};
FACEBOOK.post_to_page = function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
post_to_page(FB_PAGE_ACCESS_TOKEN);
} else {
FB.login(function(response) {
post_to_page(FB_PAGE_ACCESS_TOKEN);
}, {scope: 'manage_pages, publish_pages, publish_actions'});
}
});
}
function post_to_page(access_token_data) {
var body = 'Facebook Error: (#200) Subject does not have permission to post photos on this page.';
var data = {
message: body,
access_token: access_token_data,
url: '<image_url>'
}
FB.api('/<fb_page_id>/photos', 'post', data, function(response) {
if (!response || response.error) {
console.log(response.error);
} else {
console.log(response.id);
}
});
}
I've been trying to post some data from my webpage directly to a facebook group wall. Is is possible to achieve?
Here's a code i used:
$.getScript('//connect.facebook.net/en_UK/all.js', function(){
FB.init({
appId : myAppId,
xfbml : true,
version : 'v2.0'
});
FB.api('/groupId/feed', 'post', { link: 'linkToMyPage', name: 'Name' } , function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Success!');
}
});
Any help will be appreciated!
By comparing your code against Facebook's sample code, I noticed that you need to capitialize "POST", also their code nests { link: 'linkToMyPage', name: 'Name' }' within an Object block. So I am thinking you should replace the link and name with something similar to:
"object": {
"link": linkToMyPage,
"name": 'Name'
}
Here is the API doc, which describes how to publish to a Group feed.
Im trying to upload a photo to one of my albums on facebook, with the below code, but no upload and now error?
What are I missing?
I have a album thats called Loggfoton where I want the photos to end up, or if there is no album at all, then that one is created.
function uploadphoto(){
var imgURL =
"http://www.mydomain.se/armani1.jpg";
FB.api('/me/photos', 'post', {
message: 'photo description',
access_token: accessToken,
url: imgURL
}, function (response) {
if (!response || response.error) {
alert('Error occured:' + response);
} else {
alert('Post ID: ' + response.id);
}
}); }
Any input really appeciated!
Thanks!
Use "/album_id/photos" as url:
function uploadphoto(){
var imgURL = "http://www.mydomain.se/armani1.jpg";
FB.api('/album_id/photos', 'post', {
message:'photo description',
url:imgURL
}, function(response){
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}
Hope it helps
FB.ui(
{
method: 'feed',
name: 'some text',
link: 'some text',
picture: 'aa.jpg',
caption: 'some text',
description: 'some text',
message: 'some text'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
});
}
That code WORK fine, now i like after :
alert('Post was published.');
to be logged out from facebook, silently
HOW ?
Adding that code After the alert('post publish') did not do anything !!
FB.ui(
{ method:'auth.logout', display:'hidden' },
function() { alert("you're logged out!"); }
);
i have found : FB auth.logout is being raised after being logged in using the "server-side-workflow" (OAuth 2.0) but not sure i understand the code enough to know it do what i ask !
https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
https://developers.facebook.com/docs/reference/javascript/FB.logout/
Best Practices
FB.logout will log the user out of both your site and Facebook. You
will need to have a valid access token for the user in order to call
the function.
Calling FB.logout will also invalidate the access token that you have
for the user, unless you have the offline_access permission.
I wrote a sample using the comments box to fire the auto logout
http://shawnsspace.com/fb.logout.test.php
THE CODE:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '112104298812138',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
//channelUrl : 'http://WWW.MYDOMAIN.COM/channel.html', // channel.html file
oauth : true // enable OAuth 2.0
});
FB.Canvas.EarlyFlush.addResource("http://shawnsspace.com/index.php");
FB.Canvas.setAutoResize();
FB.getLoginStatus(function(response) {
if (response.authResponse) {
var accessToken = response.authResponse.accessToken;
} else {
}
});
FB.Event.subscribe('comment.create', function(response) {
//alert(JSON.stringify(response));
FB.logout(function(response) {
window.location.reload();
});
});
FB.Event.subscribe('auth.login', function(response) {
//top.location.href = 'http://apps.facebook.com/shawnsspace/fbcomments.php?ref=loggedin';
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
//top.location.href = "http://apps.facebook.com/shawnsspace/fbcomments.php?ref=loggedout";
alert('logged out');
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
I am using facebook javascript SDK to post comment on facebook and using this code:
FB.init({
//appId: 'YOUR_APP_ID', cookie: true,
appId: '2697XXXXXXXXXX098', cookie: true,
status: true, xfbml: true
});
FB.ui({
method: 'feed',
name: 'MyComment',
link: 'http://www.facebook.com./',
picture: 'http://xyz.com/App_Shared/Images/logo.jpg',
caption: 'Comment caption',
description: 'comment description',
message: 'Join facebook.'
}, function (response) {
if (response && response.post_id) {
alert('Post was published.' + response.post_id);
} else {
alert('Post was not published.');
}
});
and now i want to delete this post by using the same type of js code.
I saved post_id in my database.
Can you provide me the code or any URL to do this.....
First of all you must give to access to you application and allow by user according to this URL
http://developers.facebook.com/docs/reference/api/permissions/
These permission you can use like this method
FB.login(function (response) {
if (response.authResponse) {
FB.api('/me', function (response) {
jQuery('#hdfUID1').val(response.id);
jQuery('#btnFBLoginTemp').click();
// FB.logout(function (response) {
// });
});
} else {
jQuery('#hdfUID1').val('');
}
}, { scope: 'email,user_photos,read_stream,publish_stream,offline_access' });
if user allow these permissions then you can delete comment from user's wall which was posted by your application by using "Anatoly Lubarsky" answer method....
FB.api(postId, 'delete', function(response) {
// callback here
});
should be very simple like:
FB.api(postId, 'delete', function(response) {
// callback here
});
hope this helps