FB Graph API: Posting as a page, to a different page - javascript

I'm using Facebook's Javascript SDK, and I need a way to let users post to a specific page impersonating one of the pages they own. Posting as the user works perfectly, but I can seem to get this right:
var data = {
access_token: access,
message: "Hello"
};
FB.api("/" + page_id + "/feed", "post", data, function(response) {
console.log(response);
});
page_id is the numeric ID of the specific page I want to post to, and access is the access_token retrieved from the "me/accounts" api, using the manage_pages authorization.
The output I recieve from the response variable is the following object:
error: Object
code: 200
message: "(#200) Posts where the actor is a page cannot also include a target_id"
type: "OAuthException"
I couldn't find the answer to this anywhere. Is this just not possible? It can't be.

quick googling suggests it's not possible:
https://stackoverflow.com/questions/9706873/post-on-wall-fan-page-to-a-fan-page-that-likes
How to Post with Application Name
also, take a look at the old REST API documentation: https://developers.facebook.com/docs/reference/rest/stream.publish/
target_id
Note: If you specify a Page ID as the uid, you cannot specify a
target_id. Pages cannot write on other users' Walls.
Note: You cannot publish to an application profile page's Wall.

Related

Instagram API redirect uri

I am attempting to get an access token with Instagram's API but I am having trouble, and I believe the issue is because of the redirect URI.
I currently have my redirect uri as: http://socialmediasnapshot.net
However, a user is signing in from http://socialmediasnapshot.net/Instagram.html
I tried entering in the above address as a redirect uri, but it does not work. Is there a way to make it so users can authenticate from an HTML page? I am using the following code:
$.ajax({
url: 'https://instagram.com/oauth/authorize/?client_id=' + client_id + '&redirect_uri=' + redirect_uri + '&response_type=token',
dataType: 'jsonp',
success: function(req) {
alert('yay')
alert(JSON.stringify(req))
},
error: function(req) {
alert('boo')
alert(JSON.stringify(req))
}
})
According to the documentation,
https://instagram.com/developer/authentication/
Note that the host and path components of your redirect URI must match exactly (including trailing slashes) your registered redirect_uri. You may also include additional query parameters in the supplied redirect_uri, if you need to vary your behavior dynamically
So if you want this to work, I think you would have to change the registered URI as http://socialmediasnapshot.net/Instagram.html
I was looking at it completely wrong. I just needed to use window.location.href = (the Ajax URL) to update my page, and grab the token from there!

Listing all the Facebook photos a user is tagged in using response from FB.api

Right now I'm using the Facebook APIs through JavaScript. So far I can log myself in, and now I'm trying to get information about a user who logs into my website. Here is what I have so far:
FB.api('/me/photos', function(response) { console.log(response); } );
However, the response only says: "Object {data: Array[0]}", and the Array is empty.
I've also tried using this code:
FB.api('/me?fields=photos', function(response) { console.log(response); } );
The response I get from the console is: "Object {id: "10152784292783838"}".
How can I get all the photo information? The only ideas I thought of are that I'm not using an access token, but when I tried that it still didn't work. I'm also including 'user_photos' in my scope when I log the user in, but it doesn't change the response. What am I doing wrong?
As long as you're using the app's admin/tester/developer users, you should be able to get the data. If not, the array will be empty, because facebook requires an app review for apps which request more than the basic permissions.
See
https://developers.facebook.com/docs/apps/review/login#do-you-need-review

Get facebook comment by id

I have facebook comment box. I want to store the comment in a database when a user comments something. So I am attaching a callback function with FB.event.subscribe('comment.create', ... From there I get commentID and href but the only way to get the exact comment is with FQL which is deprecated from 2011 and nobody knows when facebook will remove it. Using Graph API I can get all comments but there is no way to find out which comment belongs to a specific user of our app (we don't ask for any permissions so there is no access_token; we trigger popup form when somebody comments so it is very important to match user details with comment (that's why we subscribe to comment.create)). Is there a smart way to do this or should rely on a deprecated feature?
Edit:
I am trying to get the comment like this:
FB.api(
{
method: 'fql.query',
query: "SELECT text, fromid FROM comment WHERE post_fbid = '" + resp.commentID +
"' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url='" + resp.href + "')"
},
function (data) {
var fb_id
, comment
console.log(data)
if ( data.length == 1 ) {
fb_id = data[0].fromid
comment = data[0].text
}
// ...
}
)
The problem is that when on localhost - it returns array with one element - the comment I want. When I upload to my app - then it returns array with no elements. Maybe there are permission issues. My question is how to get the content of a comment when submitted. What is the canonical way? Is it possible without access_token and permissions?
FQL isn't deprecated. The blog post is talking about Rest API specifically, later on it states changes with FQL.
To access comments you need a valid access token that can view the top level object. Assuming this is just for comments on websites, a normal extended page access token should suffice by following scenario 5 explained at https://developers.facebook.com/roadmap/offline-access-removal/
https://graph.facebook.com/oauth/access_token?
client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN
Exchange the short-lived user access token for a long-lived access token using the endpoint and steps explained earlier. By using a long-lived user access token, querying the [User ID]/accounts endpoint will now provide page access tokens that do not expire for pages that a user manages.
Then using a page access token from [User ID]/accounts you can pretty much hard code it in (you can create your own backend login tool, in the event that you invalidate the token one day or need to change it) via a server side language for example PHP using the PHP SDK
$facebook->setAccessToken('YOUR_PAGE_TOKEN');
So from here you can do an AJAX POST to the PHP page where the SDK is loaded
window.fbAsyncInit = function(){
FB.Event.subscribe('comment.create',
function(response) {
onCommentCreate(response.commentID);
}
);
function onCommentCreate(commentID) {
$.ajax({
type: 'POST',
url: 'createcomment.php',
data: {commentid:commentID},
success: function(result)
{
alert(result);
}
});
}
}
and request the comment information from there
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_POST['commentid'] )) {
$commentid = $_POST['commentid'];
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'APP_ID_HERE',
'secret' => 'APP_SECRET_HERE',
));
$facebook->setAccessToken('YOUR_PAGE_TOKEN');
$response = $facebook->api($commentid);
echo $response['from']['id'];
}
?>
References
https://developers.facebook.com/docs/graphapi/guides/comments/
https://developers.facebook.com/roadmap/offline-access-removal/

How to retrieve the email id from facebook users

I need to pull the email addresses of all the people who are in my friendlist of facebook.
Is it possible to pull all the email address of my friends using javascript api.
It should be possible to any user, if they provide authentication.
You cannot access the email addresses of a user's friends. You can get their basic public information (user_id, display picture etc.).
To get the email address of these users, they would also have to authorise your application.
you can read the content using by sending a request to the graph api(http://developers.facebook.com/docs/reference/api/) in fb with a valid user access token ....
you can retrieve the user access token via javascript SDK
after getting that request the email of that particular user using fb.uimethods
sample code
FB.api('/', 'POST', {
batch: [
{ method: 'GET', relative_url: 'me'},
...
]
}, function (response) {
console.log(response);
});
the request url shuld be something like "https://graph.facebook.com/me?access_token=user_acess_token" & this will display the basic details about the user ...

Can an access token returned by Facebook to the Javascript SDK work server-side with the PHP SDK?

I'm building a website that makes use of Facebook connect. I'm authenticating users client-side with the javascript SDK and calling an AJAX method on my server every time a user logs in to check if the user is known to my app, and if the user is new to store their FBID in my database to register them as a new user.
My question is: Can the access token returned by Facebook to the Javascript SDK be used server-side (with the PHP SDK for example)? Can I send the access token string to the server via an AJAX call, store it in my database (along with a timestamp so I know how long it's valid for) and then use it to make calls to the graph API server-side? Is this even a logical thing to do?
Yes, this should work. Look at this question: How to properly handle session and access token with Facebook PHP SDK 3.0?
This is a workaround for the old JS and new PHP SDK. In my app I send the access token generated by the JS SDK via a form to my PHP. I have no doubts that this also works by sending the access token via ajax!
Using Jquery:
//Set an error message
var oops = ("Put your something went wrong message here.");
//Function to post the data to the server
function save(uid, accessToken){
$.post("../foo/bar", { uid: uid, access_token: accessToken, etc, etc }, function(data){
alert("Successfully connected to Facebook.");
location.reload();
}, "text");
}
function handler(x){
if (x.authResponse){
var token = x.authResponse.accessToken;
var uid = x.authResponse.id;
FB.api("/me/accounts", {access_token: token},
function(response){
if(response.data.length == 0) {
//Regular facebook user with one account (profile)
save(uid, token);
}else{
//Handle multiple accounts (if you want access to pages, groups, etc)
}
});
}else{
alert(oops);
}
}
FB.login(handler, {scope: 'The list of permissions you are requesting goes here'});
Any improvement suggestions are always appreciated.

Categories