Post to facebook page as user - facebook-graph-api

Am working on facebook graph API for posting on facebook page.I have a webpage where each users can login and share contents on a facebook page.Consider certain organizations,each organization has there own facebook page.From my website any user having facebook account can come and share their feedbacks about that organization and that feedback should be posted in the facebook page of the particular organization.
I need to implement this using facebook javascript api,but am getting an error
The user hasn't authorized the application to perform this action","type":"OAuthException","code":200
Here is my code:
FB.api('/page_id/feed', 'post',
{
message : "It's awesome ...",
name : 'Feedback',
to: '',
from: '',
description : 'Your description'
},
function(response) {
if (!response || response.error) {
//alert(JSON.stringify(response.error));
console.log(JSON.stringify(response.error));
} else {
alert('Post ID: ' + response.id);
}
});
}
Please help
Thanks

Try this:
function login() {
FB.login(function (response) {
if (response.authResponse) {
// connected
postFeedBack();
} else {
// cancelled
}
}, { scope: 'publish_stream' });
}
function postFeedBack() {
FB.api('/page_id/feed', 'post', {
message: "My Feedback"
}, function (response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}

Related

How to get data from an Facebook apprequest response?

I'm trying to figure out how I can get data from an apprequest response:
function invite(provider) {
FB.ui({
method: 'apprequests',
message: 'Check this thing out!',
title: 'Request Title'
}, function(response) {
if (response && !response.error) {
console.log('3 friend invited.');
console.log(response);
} else {
console.log('You have to invite 2 friends.');
console.log(response);
}
});
}
console.log(response); returns:
Object {
request: "667131760069164",
to: Array[1]
}
request: "667131760069164"
to: Array[1] 0: "1476807679266627"
length: 1
I'm wondering how I can get the information stored in to: to check how many were invited.
Thoughts, anyone?
Try this
obj = JSON.parse(response);
console.log(obj.to[0][0]);
console.log(obj.to[0].length);
You can access first value in 'to' array by obj.to[0][0] and obj.to[0].length
I managed to get the result I wanted with:
if (response.to.length > 2) {
console.log('Success! You have invited ' + response.to.length + ' friends.');
} else {
console.log('You need to invite at least 3 friends')
}
Thanks for the suggestions.

Why is FB.Event.subscribe no longer detecting when I've logged out of facebook?

If I'm logged into facebook the testAPI() function fires as it's supposed to, but when I log out of facebook and refresh the page where my init code is, nothing happens. My console should log "not logged in", or "not authorized", but it doesn't. I've tried clearing the cache, but it doesn't work.
FB.Event.subscribe('auth.authResponseChange', function(response) {
//this works
testAPI();
} else if (response.status === 'not_authorized') {
console.log("not authorized");
} else {
console.log("not logged in");
}
});
var testAPI = function() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
};
auth.authResponseChange only fires when the status actually change, in your case it never change.
What you want is to use FB.getLoginStatus.

FB.ui method 'permissions.request' deprecated? the response always be false for me

Here is my code:
FB.ui({method: "permissions.request", "perms": 'publish_stream', 'display': 'popup'}, function (response) {
console.log(response)
console.log(response.perms)
}, {scope: 'publish_stream'});
And the result is always 'false', does anyone know the reason?
Thanks
The permissions.request dialog is not (no longer?) in the spec for FB.ui:
https://developers.facebook.com/docs/reference/javascript/FB.ui/
It seems that the only way to ensure that a user is logged in and has the required permissions for an action is to use a combination of FB.login() with the proper permission scope, and then FB.api('/me/permissions') to query the Graph:
FB.login(function(response) {
if (response.status === 'connected') {
FB.api('/me/permissions', function(response) {
if (response.data && response.data[0] && response.data[0].publish_actions) {
console.log("You got'em!");
}
});
}
}, { scope: 'publish_actions' });

Post to facebook wall from phonegap android

Am trying to post to facebook wall from an android app written with phonegap. I do get the following error: message:(#200) the user hasn't authorized the application to perform this action type:OAuthException. So my question is How do i get the user to authorize my app so i can post to their wall. I use this code to login:
function login() {
FB.login(
function(response) {
if (response.session) {
alert('logged in');
} else {
alert('not logged in');
}
},
{ perms: 'publish_stream' }
);
}
and i try to post with the below code which throws an error.
function postToWall() {
var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { body: body }, function(response) {
if (!response || response.error) {
alert('Error occured ' + JSON.stringify(response.error));
} else {
alert('Post ID: ' + response);
}
});
}
Am using the phonegap facebook api from https://github.com/davejohnson/phonegap-plugin-facebook-connect and FB.ui does not work.
Thanks
Posting the same content throws errors so use a text type input for positioning content on Facebook, with post as the text type input:
function postdata()
{
var body = document.getElementById("post").value;
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error)
{
alert('Error occured');
}
else {
alert('Post ID: ' + response.id);
}
});
}
There are serious problems with that phonegap plugin right now. I'm really wishing it would be picked back up to fix them. One of the main issues is that oauth2 isn't supported and the plugin is using an older facebook sdk.

Facebook graph app requests

I dont know why but https://graph.facebook.com/132333806850364?access_token=218237124854683|VLsQV-ec2paQpq5hVMFFjtnk9_w returns false even though the app token is valid. The invitation was sent properly and I recieve invitations as well.
//Single user REquest
function sendRequestSingle(c_id,page_id,sendto,from,g_id) {
FB.ui({
method: 'apprequests',
to: sendto,
message: 'Win Mega Prizes!',
title: 'Enter in this contest!',
data: c_id+','+page_id+','+g_id
},
function (response) {
if (response && response.request_ids) {
var requests = response.request_ids;
window.location.href = "http://www.serverhere.com/contest/handle_invitations.php?invitations="+requests+"&contest_id="+c_id+"&page_id="+page_id+"&user="+from+"&g_id="+g_id+"&sendto="+sendto+"&single=1";
} else {
alert('canceled');
}
});
return false;
}