Facebook Graph API Posting a Comment as User - facebook

I am trying to build a comments method within a website without the Facebook comments plugin.
It's a photogallery portion of a website and all the photos are actually hosted on the business Page, I'm doing this to make use of FBs uploading, resizing/rotate etc rather than having to build that myself too.
As a happy bi-product to this I figured I could link comments on the website with comments on FB and have a syncrhonised comments system whereby comments posted on either site (my website or FB) will show equally on either site.
So, I have worked out how to post the comments, but it is always posting as the Business Page, not the logged in user.
This code posts the comment, the variable obj is the ID of the photo on Facebook. msg is obviously the comment text. I have setup methods to get the correct permissions where required which are called if the response from the post states that this is needed.
FB.api('/' + obj + '/comments',
'post',
{ "message": msg,
"access_token": accessToken,
"from": uid
}, // do the post
function (response) { // check the response
if (!response || response.error) { // if there's an error
// DO STUFF WITH ERRORS/REQUEST PERMS ETC
} else {
alert("Msg posted id: " + response.id);
};
});
Everything above works, but when the comment appears, it is always showing from: my Page ID not, what I need which would be from: my user id
accessToken and UID is being retrieved as follows:
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
}
});
The para from in the first function there posts the User ID, NOT the page ID, but whether that para is there or not, it always posts the comment as the Page.
It's frustrating the hell out of me as so far this has been pretty simple to achieve!

Related

Share an image to Faceboook

I need to share an image to facebook. For this I am trying the API call suggested here
FB.login(function(response) {
if (response.authResponse) {
FB.api(
"/{page-id}/photos",
"POST",
{
"url": "https:\/\/www.facebook.com\/images\/fb_icon_325x325.png",
"published": "false"
},
function (response) {
if (response && !response.error) {
console.log(JSON.stringify(response))
/* handle the result */
}
else
{
console.log("ERROR: " + JSON.stringify(response))
}
}
);
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
But I am getting an error (#803) Some of the aliases you requested do not exist: {page-id}
Is page-id the same as the userID returned in the authResponse? Because I also tried hardcoding that id, but I got a deprecated API message.
I need to allow the user to share the photo directly to his feed.
Thanks
Page ID and User ID are not the same. You get a User ID after authorization, you get a Page ID by using the /me/accounts endpoint with a User Token. You can also get Page Tokens with /me/accounts?fields=access_token. You need to use a Page Token with the publish_pages and manage_pages permissions to create posts or photos on a Page you manage.
As WizKid commented, you can also just use /me/photos to upload photos. Just use a Page Token.
More information: https://developers.facebook.com/docs/facebook-login/access-tokens/

Get the location id of a fan page

I have a fan page with an address.
Also, I can get the location details using the facebook API.
However, to create a new post with a review button I need the location id instead of the details.
I am using the following lines to create a share button:
var data = {
message: 'Hi !',
place: location_id
};
FB.api('/me/feed', 'post', data, function(response) {
if (!response || response.error) {
console.error(response.error);
} else {
console.log(response.id);
}
});
And it works if I use a specific location id. But I can't get the location id from a specific page.
Please give me some ideas. Thank you!
I was falling asleep, but I got one idea.
And finally, that idea was the solution:
It is possible to use the fan page id directly in the place parameter. It can be used like a place id.

Facebook birthday is not coming when logging in

I am using Facebook login button with JavaScript. Everything is working fine but my client needs to fetch "birthday" when any user log-in with Facebook.I have written the code but for some fb ids it is giving me the Birhtday while in some cases it is not giving.
I have checked answers on google already and have done almost everything but no success.
Here is my code I am using
HTML CODE
<a onclick="fb_login();" href="javascript:void(0);"><i class="fa fa-facebook"></i> <span>Log in with Facebook</span></a>
JavaScript Code
function fb_login(){
FB.login(function(response) {
if (response.authResponse) {
//console.log('Welcome! Fetching your information.... ');
//console.log(response); // dump complete info
access_token = response.authResponse.accessToken; //get access token
console.log(access_token);
user_id = response.authResponse.userID; //get FB UID
FB.api('/me', function(response) {
console.log(response);
user_email = response.email; //get user email
first_name = response.first_name; //get first name
last_name = response.last_name; //get last name
full_name = response.name; //get last name
fb_id = response.id; //get id
birthday = response.birthday; //get id
alert(birthday);return false;
// you can store this data into your database
var data = {
action: 'mb_facebook_login',
user_email:user_email,
first_name:first_name,
last_name: last_name,
full_name:full_name,
fb_id:fb_id,
birthday:birthday
};
jQuery.post(ajaxurl, data, function(ajaxresponse) {
console.log(ajaxresponse);
if(ajaxresponse=='loggedin')
{
//location.reload();
}
});
});
} else {
//user hit cancel button
console.log('User cancelled login or did not fully authorize.');
}
}, {
scope: 'user_birthday,email,user_location'
});
}
I have also made my birthday to public also set the permission here but it´s not working.
Please help me in this.Thanks in advance!
https://developers.facebook.com/docs/apps/changelog
Apps requesting more than public_profile, email and the user_friends permission must be reviewed by Facebook before those permissions can be requested from people.
That is one reason why it does not work for some people, without review it will only work for users with a role in the App (developer, tester, admin).
Another reasons could be that some users did not set a birthday. Not sure if that is possible, but it is worth testing.

Post a message on Facebook using FB.api

I write down a code in javascript for posting message on Face-Book. I posted it successfully but it's always showing only me privacy statement.
I really want to post this message publicly.
I try to set privacy = { 'value': 'EVERYONE' }; . still message posting private(only me).
My code is-
var privacy = { 'value': 'EVERYONE' };
var txt = 'my post to test feed post using api';
FB.api('me/feed', 'post', { message: txt, privacy: privacy }, function (response) {
if (!response || response.error) {
alert(JSON.stringify(response.error));
} else {
alert('Post ID: ' + response.id);
}
});
How can i post it publicly ?
thanks a bunch for your valuable help.
Did you manually set the privacy of the application when you added your application? If you go to your Facebook Settings, see what you set your application to. The privacy parameter is restricted to what the user sets on their account.
E.g. If the user has set "Friends", but the app uses "Public", then the user's preference is used. But if the user has set "Public", but the app uses "Friends", then the app's setting is used, as it's more restrictive than the User's settings.
In short, the most restrictive privacy setting between the User and Application will take priority.

JS SDK getLoginStatus doesn't return userID nor signedRequest

I'm using PhoneGap/Cordova with the facebook plugin. Everything seems to work except for getLoginStatus who is not working as defined here and here. When called, it returns some data, but not all: it doesn't return userID nor signedRequest.
Here is the code:
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
var fb_uid = response.authResponse.userID;
var fb_signedRequest = response.authResponse.signedRequest;
alert('logged in');
} else {
alert('not logged in');
}
});
userID is filled with ellipsis (...), while signedRequest is undefined.
I managed to get userID with a graph call to /me:
FB.api('/me', function(me){
if (me.id) {
var fb_uid = me.id;
}
});
I wasn't able to find any way in the documentation to get a signed_request, which I have to use to authenticate the facebook user to a remote service to whom the user already connected to with facebook (I already made a login call so user is OK).
Basically the problem is that my call to getLoginStatus returns
{
status: 'connected',
authResponse: {
session_key: true,
accessToken: 'a long string...',
expiresIn:'a number',
sig: '...', //exactly this string
userID:'...' //exactly this string
secret:'...' //exactly this string
expirationTime:'a long number'
}
}
instead of what documented
As a background, when authentication happens using the plugin then the JavaScript SDK API calls the iOS/Android SDK to handle the authorization and then pass response auth data back to the JS part. The native (iOS/Android) SDKs do not get signed requests back to be able to pass this on to the JS. This is why it's empty.
If you use the latest plugin you should at least now be seeing the user ID. The one from June likely did not pass this back. Otherwise as a work around, you could perform a call to the /me endpoint when authentication is successful in your JS code to get the user id. See the Hackbook example that does this.