How to post a custom post using facebook javascript api - javascript

Okay i have a website now with an input and button. When the user clicks a button, I want his input to be as a new feed on his facebook account.
I already have added the facebook javascript sdk and the graph api one
and I created a successful login button and a new feed button sending a standard text which "is this is a text feed".
So how can i post what the user inputs?
The code of posting is
function postshare() {
FB.api(
"/me/feed",
"POST",
{
"message": "this is a test post"
},
function (response) {
if (response && !response.error) {
/* handle the result */
document.getElementById('poststt').innerHTML = 'post shared succesfully';
}
}
);

If you have a text input on the page with an id, you can reference that input and get its value using document.getElementById. So if you had this input
<input type="text" id="fb_input" />
on your page, you could then do this:
function postshare() {
var message = document.getElementById("fb_input").value;
FB.api(
"/me/feed",
"POST",
{
"message": message
},
function (response) {
if (response && !response.error) {
/* handle the result */
document.getElementById('poststt').innerHTML = 'post shared succesfully';
}
}
);
}

Related

Facebook GET /v2.6/{friend-list-id}/members Return Blank in API v2.6

FB.login(function(response) {
console.log('Welcome! Fetching your information.... ');
FB.api(
"/me/friendlists",
function (response) {
if (response && !response.error) {
$.each(response.data, function (key,value) {
//console.log(value.id);
FB.api(
"/"+value.id+"/members",
function (response) {
if (response && !response.error) {
console.log(JSON.stringify(response));
/* handle the result */
}
}
);
});
console.log(JSON.stringify(response));
/* handle the result */
}
}
);
I am Using this code to get friendlist in facebook latest API.
That return blank array in return. I got all my FriendList Id. But after call /members that return blank.
I am testing with "Test application and use 'read_custom_friendlists' permission"
I guess you want to get a list of friends? You can only get friends who authorized your App with the user_friends permission, and this would be the API call: /me/friends.
More information: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app

Facebook Delete Comment API problems (#200) App does not have sufficient permission for this action for remove comments

I Logged-in with user that I am facebook pages administrator. I write api to delete spam comments in posts by use this method.
$scope.deleteComments = function (commentID) {
if (confirm("Confirm Delete Comments")) {
FB.api(
"/" + commentID,
"DELETE",
function (response) {
console.log(response, commentID);
if (response && !response.error) {
/* handle the result */
}
}
);
}
};
I followed instructions from "https://developers.facebook.com/docs/graph-api/reference/v2.5/comment". Before I use Facebook API I given and Permission with this code.
$scope.triggerLogin = function () {
FB.login(function () {
$scope.checkLoginState();
}, {
scope: "public_profile, publish_pages, manage_pages"
});
};
so that while I use deleteComments() function I get error object from facebook like this
error: Object
code: 200
message: "(#200) App does not have sufficient permission for this action"
type: "OAuthException"
Does anyone know how to fixed this problems, Thank you.
You are most likely not using a Page Token, right now it looks like you are using a User Token. You MUST use a Page Token to delete comments. Generate a Page token by using the /me/accounts endpoint (or /page-id?fields=access_token for a specific Page) and use it in the API call:
FB.api(
'/' + commentID,
'DELETE',
{access_token: 'your-page-token'},
function (response) {
console.log(response, commentID);
if (response && !response.error) {
/* handle the result */
}
}
);
More information about Tokens and how to generate them:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

Posting to facebook wall using graph api

I'm trying to post a text with image to facebook wall using graph api.
I'm using following code snippet for this.
var body = {
message : 'this is a test message',
image : 'http://someurltoimage.png'
};
FB.api(
"/me/feed",
"POST",
{
"object": {
"message": body.message,
"picture": body.image
}
},
function (response) {
if (response && !response.error) {
//process when success
}
}
);
But I'm getting following error code.
error: Object
code: 100
error_subcode: 1349125
message: "Invalid parameter"
type: "FacebookApiException"
There's no document for this error.
Any advise will be appreciated.
"I'm trying to post a text with image to facebook wall using graph api."
You are using /feed, to upload a photo you have to use /photos call
You are sending an invalid parameter object which contains your parameters, to Facebook, the API doesn't know that your parameter Object is an object (I know, too many objects here, in another way, you're sending an object within an object
To solve all this, replace me/feed with me/photos and the 3rd argument (your object) with the body
var body = {
message : 'this is a test message',
url: 'http://someurltoimage.png'
};
FB.api("/me/photos",
"POST",
body,
function (response) {
if (response && !response.error) {
//process when success
}
}
);

Cakephp Ajax Login is loggin in even with wrong data

I have a simple html form to login outside my cake structure, I send the login data via jquery to my login function:
public function login() {
if ($this->request->is('ajax')) {
$this->request->data['Appuser']['password'] = Security::hash($this->request->data['password']);
$this->request->data['Appuser']['name'] = $this->request->data['name'];
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl(array('action' => 'returnUserData')));
}else{
$this->response->body(json_encode('Login failed!'));
}
}elseif($this->request->is('post')){
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}else{
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
The very strange thing is:
the login via post from /view/users/login.ctp form works perfectly!
When I try to login from "outside" via ajax, I am always logged in even with wrong access details and get the data from function 'returnUserData'
$('#login').click(function() {
$.ajax({
type: "POST",
url: '/api/login',
data: {
name: $("#username").val(),
password: $("#password").val()
},
success: function(data)
{
alert(data);
}
});
});
When I set a debug(); after if ($this->request->is('ajax')) I can see it, so the script seems to go into the right if section. But I don't get it, why the $this->Auth->login always return true...?

Is there an event in the Facebook JavaScript SDK that fires after a user has commented?

I'm using the following to display a coupon for users that have liked my company's page via a Facebook 'Like' button on your page.
FB.Event.subscribe('edge.create',
function(response) {
// action
}
);
};
When a user clicks the 'Like' button the box appears asking if they want to post it to their wall with a message and the callback takes place at this time to display the coupon.
Is there a callback that occurs after the user has posted (or opted not to post)?
I use something like this for posts then check the response:
<script>
function fb_share() {
FB.ui({
method: 'feed',
name: 'Name of App',
link: 'http://YOURLINK.com',
picture: 'someurltoimage.jpg',
description: 'Your description of the post for the wall!'
},
function(response) {
if (response && response.post_id) {
console.debug(response.post_id);
// Post was published - now lets send it to our ouwn custom processing page on our server
$.post('/process-post',{uid: <?php echo $currentUser; ?>, wallpost: response.post_id},function(resp) {
// callback after storing the requests
});
} else {
// Post was not published;
}
});
return false;
}
</script>

Categories

Resources