Facebook Graph API: How to read the "likes" of a page? - facebook

I try to read the list of people who likes a page, but it looks like it is not possible with the Graph API or FQL. I found lots of entries of people trying it, but nobody found a solution. Is it possible?
Here I get the profile info, I see the field "likes" with the number 200
https://graph.facebook.com/162253007120080/
But when I try to read it, I get an empty data array?
https://graph.facebook.com/162253007120080/likes
Anybody an idea how to handle this?

HI,
here you go, streight out my tool box:
<?php
require_once('../library/Facebook.php');
$facebook = new Facebook(array(
'appId' => '123456789000000',
'secret' => 'asdf',
'cookie' => true,
));
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => 'select fan_count from page where page_id = 1234567890;'
));
echo $result[0]['fan_count'];
?>

Call the graph api link. Extract the contents using json_decode.

The resulting data array isn't empty when you go there, it is:
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException",
"code": 104
}
}
Which means you need an access token in order to access that LIKE data..

Related

Post to facebook page from facebook page tab app

I want to post activities from page tab app to my own facebook page. I referred many documents to post message to app.
My php code :
$GLOBALS["facebook"] = new \Facebook_Facebook(array('appId' => $facebook_app_id, 'secret' => $facebook_secret,));
$page_info = $GLOBALS["facebook"]->api("/pageid?fields=access_token");
print_r($page_info);die;
if (!empty($page_info['access_token'])) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'TEST'
);
$postId = $facebook->api("/pageid/feed", "post", $args);
But pageinfo variable has only id. I'm not getting access_token. Any idea what else need to be done?
We need manage_pages permission which we can get it using https://www.facebook.com/dialog/oauth?client_id=client_app_id&client_secret=9ea3e2eff7c65b1fcf1a633da&redirect_uri=YOUR_REDIRECT_URL&scope=read_stream,publish_stream,offline_access,manage_pages.
After that using above code we will get access token which can be used to publish to our page

Direct creation of this object type is forbidden - 1611181

So I'm trying to get it to post a sample photo using /me/photos.upload with the Open Graph API. I'm getting the following error
{"error":{"type":"Exception","message":"Direct creation of this object type is forbidden.","code":1611181}}
I have looked all over, and have no clue what that means. Any ideas?
EDIT: Code:
include 'facebook.php';
include 'cURL.php';
$facebook = new Facebook(array(
'appId' => 'REMOVED',
'secret' => 'REMOVED',
));
$access_token = $facebook->getAccessToken();
$fields = array( 'access_token' => $access_token,
'photo' => 'http://treesroulette.com/app/test.html'
);
$result = HTTP_POST("https://graph.facebook.com/me/photos.upload",// URL to query
$fields, // POST fields; associative array
USER_AGENT, // user-agent value
"", // cookie storage and retrieval
"", // proxy; type:ip:port[:user:pass]; supported types: http, socks5
true, // return the data or not
false, // include headers in the return data
"", // set value for REFERER header
true, // automatically follow "redirects" ("Location" header)
false);
echo $result;
Your code is wrong: there's no such endpoint as /me/photos.upload - the old REST API had a method called photos.upload but this isn't related to the current API's structure
Did you see that in the documentation? If so please add a comment below and i'll fix it.
You should be posting to /me/photos
See the photos connection of the User object or this guide to uploading photos in the blog
Also, change your app secret immediately as you pasted your code without removing it
If you wants to upload photos and display your own icon I recommend you to read this thread: Show custom Facebook app icon in news feed on Page photo upload

Get user's country at Facebook: getSignedRequest() results without 'user' field

Code:
$facebook = new Facebook(array(
'appId' => some_fb_app_id),
'secret' => some_fb_app_secret),
'cookie' => true,
));
$result = $facebook->getSignedRequest();
echo var_export($result,1);
Result:
array (
'algorithm' => 'HMAC-SHA256',
'code' => 'A.....l',
'issued_at' => 1354720771,
'user_id' => 'some_user_id',
)
I expect $result["user"]["country"] to be set. How can I fix this problem?
ps. by Facebook Graph API - how to get user country?
According to the facebook PHP SDK getSignedRequest() only gives you the signed request, just like the name suggests. User info is usually gotten through the Graph API, using calls like
$facebook->api('/me','GET');
which gives you the profile for the user associated with the access_token you are using.
Note that you will only ever get information that you have the permissions for, so if you haven't added a particular bit of info beyond the basics to your apps scope, you will not get it.

Facebook API post to friends wall

I'm building application to post statuses from authenticated users like that one.
The problem is the application posting from my profile on friends wall who authenticated the application from my profile.
I'm using Facebook API with CodeIgniter.
the code
$userId = $this->facebook->getUser();
$IDs = $this->facebook_data->get_IDs();
if(isset($_POST['link'])){
$args = array(
'from' => array('name'=>'Resala','id'=>'294357147348261'),
'application'=>array('name'=>'XXXX','id'=>'XXXXX'),
'name' =>'XXXX',
'message' => $_POST['status2'],
'link' => $_POST['link']
);
}else{
$args = array(
'from' => array('name'=>'Resala','id'=>'294357147348261'),
'application'=>array('name'=>'Resala','id'=>'XXXXX'),
'name' =>'XXXX',
'message' => $_POST['status1']
);
}
foreach($IDs->result() as $row){
$ID=$row->user_id;
$this->facebook->api("/$ID/feed", "post", $args);
}
The right docs for creating a post are:
http://developers.facebook.com/docs/reference/api/user/#posts
There you can see the params "from" and "application" don't exist and "name (can only be used if link is specified)".
It's not possible to post in the name of an app. You can only post in the name of an page (that may be assosiated with the app).
Therefor you have to use the page access token instead of the user access token.
See: http://developers.facebook.com/docs/reference/api/page/#page_access_tokens

Facebook stream publish to wall as App/Page

i wonder if it is possible to post to a users stream/wall by using stream_publish as a page or an app. Currently I've granted my app to stream_publish on my own wall. But when I post to my stream from the app the post is displayed like I would've posted it by myself instead of having the app posted it to my wall.
I want to have the post looks like the app posted it. I already used the uid-parameter in the stream_publish params and set it to the appId ... but the post still looks like an own post.
my code:
$postData = array( $feed_dir = '/'.$fb_uid.'/feed/',
$method = 'POST',
$msg_body = array( 'access_token' => $arrUserData['access_token'],
'message' => 'here goes the message',
'name' => '-name-',
'link' => 'http://some-url',
'description' => '-description-',
'actions' => ($actions),
'uid' => $iniHandler->getIniSetting('facebook.app.id')
)
);
try {
$res = $facebook->api($feed_dir, $method, $msg_body);
}
catch (Exception $e) {
$err_str = $e->getMessage();
}
Or is ther another way to post to a wall and let the post looks like a post by an app? My users can sign up for issues in my app and my app posts some news to the users wall ... thats my aim.
You also need manage_pages permissions. Once you have those, HTTP Get me/accounts and that will be a list of pages that the logged in user is admin of. In that list will be a unique page access_token. Use that token to http post a new wall/stream item.