Access token for a simple FQL retrieving the number of status for a page - facebook-fql

I'm doing a really simple PHP app using the latest Facebook PHP SDK which aims to display the number of status a page of mine has.
To do so I created an app to have the app id and the app secret but after I'm kinda lost.
I thought I needed an app secret token so I first tried like this:
<?php
public function getFacebookPosts() {
require __DIR__ . '/libs/facebook-sdk/facebook.php';
$appId = 'myID';
$appSecret = 'mySecret';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
));
$token = $this->getFacebookAppToken($appId, $appSecret);
try {
$jinnove = $facebook->api('/my.page');
$fql = '/fql?q=SELECT+status_id+FROM+status+WHERE+uid=' . $jinnove['id'] . '&' . $token;
var_dump($facebook->api($fql));
} catch(FacebookApiException $e) {
var_dump($fql, $e);
}
}
/**
* Function to Get Access Token from Facebook
* #param $appId
* #param $appSecret
* #return string
*/
protected function getFacebookAppToken($appId, $appSecret)
{
$args = array(
'grant_type' => 'client_credentials',
'client_id' => $appId,
'client_secret' => $appSecret
);
$ch = curl_init();
$url = 'https://graph.facebook.com/oauth/access_token';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
return $data;
}
But it returns me an error 102 with the following message: "A user access token is required to request this resource.".
So then I asked on IRC and someone told me I need a user access token to do that.
Of what I've understood a user access token can only be generated when a user explicitly log into facebook to authorize this app and renew the token sometimes.
Is that true? Is there no way to use a token which doesn't imply the user to be logged? Basically anyone can view this number of status, even people who don't have a Facebook account and I want no UI dialog at all.

For some reason, Facebook has decided that querying a status can only be done by a user.
You can get around this by querying the stream table, and only returning posts with type = 46, which are status updates:
SELECT post_id FROM stream WHERE source_id= YOUR_PAGE_ID AND type = 46 LIMIT 100
The stream table has a lot of restrictions on it. Even with a high LIMIT, you may not get all the status updates if the page has been around for a while.
You can also speed up your program by cutting the number of API calls from 3 to 1 with the following changes:
If you want to get a page's ID from its username replace = YOUR_PAGE_ID in the above query with IN (SELECT id FROM profile WHERE username = 'YOUR_PAGE_USERNAME')
You don't need the 'getFacebookAppToken()` function. The PHP SDK will automatically get you an app access token if you don't have an authenticated user.

Related

Facebook API upload photo from URL

I have a website with photo gallery and I'd like to upload each photo (one by one) to my facebook page (not wall). I managed to post a message but now I want to upload a photo to a FB Page Wall by uploading an existing image from the server - specific URL (I don't want to upload again locally). Is this possible?
Yes you can do it
Example
In Graph Api Explorer
Make the call post, set url to https://graph.facebook.com/me/photos,
Add field with key message and value "any custom message"
Add another field with key url and value https://appharbor.com/assets/images/stackoverflow-logo.png
click submit
You need to know the album id and make call POST to:
https://graph.facebook.com/albumid/photos?access_token=$access_token
You will find the album id entering into the album and looking at the URL. Will be something like https://www.facebook.com/media/set/?set=a.XXXXXXXXXXX.YYYY.ZZZZZZZZZZ&type=3
Your album id are the XXXX.
this is what I use:
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true,
'fileUpload' => true,
));
$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.
$facebook->setFileUploadSupport(true);
if($user == 0) {
// If the user is not connected to your application, redirect the user to authentication page
/**
* Get a Login URL for use with redirects. By default, full page redirect is
* assumed. If you are using the generated URL with a window.open() call in
* JavaScript, you can pass in display=popup as part of the $params.
*
* The parameters:
* - redirect_uri: the url to go to after a successful login
* - scope: comma separated list of requested extended perms
*/
$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => PERMISSIONS_REQUIRED));
echo ("<script> top.location.href='".$login_url."'</script>");
} else {
// if the user is already connected, then fetch access_token and user's information or show some content to logged in user.
try
{
$access_token = $facebook->getAccessToken(); // Gives you current user's access_token
$user = $facebook->api('/me'); // Gets User's information based on permissions the user has granted to your application.
} catch(FacebookApiException $e){
$results = $e->getResult();
// Print results if you want to debug.
}
}
$img = './upload/'.$image_path;
$args = array(
'message' => 'Some Message',
'access_token'=>urlencode($access_token),
);
$args[basename($img)] = '#'.realpath($img);
$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
$response = json_decode($data,true);
$config = array('appId' => $config['App_ID'],'secret' => $config['App_Secret']);
$facebook = new Facebook($config);
// sets our access token as the access token when we call
// something using the SDK, which we are going to do now.
$facebook->setAccessToken($access_token);
$page_id = "XXXXXXXXXXXXXXX";
$page_access_token = "";
$result = $facebook->api("/me/accounts");
foreach($result["data"] as $page) {
if($page["id"] == $page_id) {
$page_access_token = $page["access_token"];
break;
}
}
$facebook->setFileUploadSupport(true);
$photo = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
$args = array(
'access_token' => $page_access_token,
'message' => "message here",
'url' => $photo,
);
$post = $facebook->api("/$page_id/photos","post",$args);

upload photo to page timeline Facebook SDK

I made this small app that basically asks a user to login and post a photo to a FB page.
the code I have works great for posting on your own wall, but when it comes to posting to a page I am having some difficulties.
require_once('../src/facebook.php');
$config = array(
'appId' => 'XXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXX',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = realpath("mypic.png"); // Path to the photo on the local filesystem
$message = 'Photo upload via the PHP SDK!';
if($user_id) {
try {
$ret_obj = $facebook->api('/PAGE_ID_HERE_??/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message,));
if i use feed instead of photos, like here
$facebook->api('/PAGE_ID_HERE_??/FEED',
it works, but only posts the message.
i have all permissions needed:
user_photos user_videos publish_action
manage_pages publish_stream
To interact with the page you need the access token of the user. The user needs to be the 'manager' or 'content creator' of the page. And those tokens are per user per page.
I have a PHP example where this is handled using a cURL call. I think it gives the idea.
$ch = curl_init();
$url = "https://graph.facebook.com/" . $album_id . "/photos?access_token=" . $access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$retdata = curl_exec($ch);
echo($retdata);
In this one the $album_id is the album id of the page and the $access_token is the access token for the user for the page.
Also here's an example by Facebook for a personal album. You can chage the album ID and the token and use that approach.

Login on facebook using username and password. Is there any way to do that?

I have a mobile application where I want the user to send to the server his facebook credentials infos and the server will act in behalf of him in facebook (get friends list and so on).
I am researching about it, but I only find information about this oauth2, where the user logs himself in Facebook in a special link, so my app can access his FB information. This doesn't help me because I would like to access this information on the server side, not by the app itself. Is there any way that I can log in using username and password?
I have a WP7 application for facebook chat where I enter my username and password and I connect, so it should be some way to perform it, correct?
edit
Because it is against the TOS, I am thinking about doing the following:
My server sends to my client the URL so the user can login and allow my app to access its informations. After that, my server accesses it.
Would that be possible?
FB is removing the offline_access token.
No solution for this question i guess.
No offline_access, token expires in 60days, No access with username and password(AGAINST TOS)
This is against their tos, but you could use
<?php
/* EDIT EMAIL AND PASSWORD */
$EMAIL = "";
$PASSWORD = "";
function cURL($url, $header=NULL, $cookie=NULL, $p=NULL)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_NOBODY, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if ($p) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
}
$result = curl_exec($ch);
if ($result) {
return $result;
} else {
return curl_error($ch);
}
curl_close($ch);
}
$a = cURL("https://login.facebook.com/login.php?login_attempt=1",true,null,"email=$EMAIL&pass=$PASSWORD");
preg_match('%Set-Cookie: ([^;]+);%',$a,$b);
$c = cURL("https://login.facebook.com/login.php?login_attempt=1",true,$b[1],"email=$EMAIL&pass=$PASSWORD");
preg_match_all('%Set-Cookie: ([^;]+);%',$c,$d);
for($i=0;$i<count($d[0]);$i++)
$cookie.=$d[1][$i].";";
/*
NOW TO JUST OPEN ANOTHER URL EDIT THE FIRST ARGUMENT OF THE FOLLOWING FUNCTION.
TO SEND SOME DATA EDIT THE LAST ARGUMENT.
*/
echo cURL("http://www.facebook.com/",null,$cookie,null);
?>
http://www.daniweb.com/web-development/php/code/290893
A solution you could use that is TOS friendly would be to have the user sign up on your website and grant the offline_access token using the Facebook SDK. Your mobile app could use that token to make requests on behalf of the user in you mobile app. I would be clear as your requesting the permission with what you intend to with it.

Using Facebook Graph API from a mobile application

I have a small card game at Facebook (and few Russian social networks), which gets user's id, first name and avatar through the old REST API.
Now I'm trying to develop the same game as a mobile app with Flex Hero SDK for Android and iPhone. Which means I can't use native SDKs for those platforms, but have to use OAuth as descibed at Facebook page.
I'm trying to write a short PHP script, which would return the user information as XML to my mobile app. My script can get the token already:
<?php
define('FB_API_ID', 'XXX');
define('FB_AUTH_SECRET', 'XXX');
$code = #$_GET['code'];
# this is just a link for me, for development puposes
if (!isset($code)) {
$str = 'https://graph.facebook.com/oauth/authorize?client_id=' . FB_API_ID .
'&redirect_uri=http://preferans.de/facebook/mobile.php&display=touch';
print "<html><body>$str</body></html>";
exit();
}
$req = 'https://graph.facebook.com/oauth/access_token?client_id=' . FB_API_ID .
'&redirect_uri=http://preferans.de/facebook/mobile.php&client_secret=' . FB_AUTH_SECRET .
'&code=' . $code;
$ch = curl_init($req);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($ch);
if (curl_errno($ch))
exit('Download failed');
curl_close($ch);
parse_str($page, $data);
#header('Content-Type: text/xml; charset=utf-8');
#print('<?xml version="1.0"? ><app>');
print_r($data);
#print('</app>');
?>
This works well and I get back the token:
Array
(
[access_token] => 262578703638|2.OwBuoa2fT5Zp_yo2hFUadA__.3600.1294904800-587287941|ycUNaHVxa_8mvenB9JB1FH3DcAA
[expires] => 6697
)
But how can I use this token now to find the user's name and avatar and especially I'm confused by how will I get the current user id?
While using REST API I've always known the current user id by calling $userid=$fb->require_login()
See docs at: http://developers.facebook.com/docs/api
Use curl to request: https://graph.facebook.com/me/?access_token=XXXXXXX
The "/me" will get you all of the info you need.
$req = 'https://graph.facebook.com/me/?access_token=' . $access_token;
$ch = curl_init($req);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($ch);
if (curl_errno($ch))
exit('Download failed');
curl_close($ch);
$user_object = json_decode($page);
var_dump($user_object);
Will return something like:
object(stdClass)#1 (20) {
["id"]=>
string(10) "1017193642"
["name"]=>
string(19) "Lance Scott Rushing"
["first_name"]=>
string(5) "Lance"
["middle_name"]=>
string(5) "Scott"
["last_name"]=>
string(7) "Rushing"
["link"]=>
string(36) "http://www.facebook.com/LanceRushing"
.....
Since I cannot add comments I'll answer here.
In order to get the profile picture you need to request https://graph.facebook.com/ID/picture. If you want only specific fields you can specify it this way: https://graph.facebook.com/ID?fields=uid,name,etc&access_token=token
Alternatively you can use the PHP SDK to authorise and log in the user so that you don't have to get the token manually - it would simplify your code. For instance, instead of every cURL request you could just do $facebook->api(request); A good description and example are here http://apps.facebook.com/graphapidemo/.

How to tag photos in facebook-api?

I wanted to ask if/how is it possible to tag a photo using the FB API (Graph or REST).
I've managed to create an album and also to upload a photo in it, but I stuck on tagging.
I've got the permissions and the correct session key.
My code until now:
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$token = $session['access_token'];//here I get the token from the $session array
$album_id = $album[0];
//upload photo
$file= 'images/hand.jpg';
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$album_id.'/photos?access_token='.$token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
//returns the id of the photo you just uploaded
print_r(json_decode($data,true));
$search = array('{"id":', "}");
$delete = array("", "");
// picture id call with $picture
$picture = str_replace($search, $delete, $data);
//here should be the photos.addTag, but i don't know how to solve this
//above code works, below i don't know what is the error / what's missing
$json = 'https://api.facebook.com/method/photos.addTag?pid='.urlencode($picture).'&tag_text=Test&x=50&y=50&access_token='.urlencode($token);
$ch = curl_init();
$url = $json;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_exec($ch);
} catch(FacebookApiException $e){
echo "Error:" . print_r($e, true);
}
I really searched a long time, if you know something that might help me, please post it here :)
Thanks for all your help,
Camillo
Hey,
you can tag the Picture directly on the Upload with the GRAPH API,
see the example below:
This Method creates an array for the tag information, in this examples the method becomes an array with Facebook user Ids:
private function makeTagArray($userId) {
foreach($userId as $id) {
$tags[] = array('tag_uid'=>$id, 'x'=>$x,'y'=>$y);
$x+=10;
$y+=10;
}
$tags = json_encode($tags);
return $tags;
}
Here are the arguments for the call of the GRAPH API to upload an picture:
$arguments = array(
'message' => 'The Comment on this Picture',
'tags'=>$this->makeTagArray($this->getRandomFriends($userId)),
'source' => '#' .realpath( BASEPATH . '/tmp/'.$imageName),
);
And here is the Method for the GRAPH API call:
public function uploadPhoto($albId,$arguments) {
//https://graph.facebook.com/me/photos
try {
$fbUpload = $this->facebook->api('/'.$albId.'/photos?access_token='.$this->facebook->getAccessToken(),'post', $arguments);
return $fbUpload;
}catch(FacebookApiException $e) {
$e;
// var_dump($e);
return false;
}
}
The argument $albId contains an ID from an Facebook Album.
And if you want to Tag an existing Picture from an Album you can user this Method:
At First we need the correct picture ID from the REST API, In this example we need the Name from an Album wich the Application has create or the user wich uses this Application.
The Method returns The Picture ID From the last Uploaded Picture of this Album:
public function getRestPhotoId($userId,$albumName) {
try {
$arguments = array('method'=>'photos.getAlbums',
'uid'=>$userId
);
$fbLikes = $this->facebook->api($arguments);
foreach($fbLikes as $album) {
if($album['name'] == $albumName) {
$myAlbId = $album['aid'];
}
}
if(!isset($myAlbId))
return FALSE;
$arguments = array('method'=>'photos.get',
'aid'=>$myAlbId
);
$fbLikes = $this->facebook->api($arguments);
$anz = count($fbLikes);
var_dump($anz,$fbLikes[$anz-1]['pid']);
if(isset($fbLikes[$anz-1]['pid']))
return $fbLikes[$anz-1]['pid'];
else
return FALSE;
//var_dump($fbLikes[$anz-1]['pid']);
//return $fbLikes;
}catch(FacebookApiException $e) {
$e;
// var_dump($e);
return false;
}
}
Now you have the correct picture ID From the REST API and you can make your REST API CALL to tag this Picture $pid is the Picture from the Method getRestPhotoId and $tag_uid is an Facebook userId:
$json = 'https://api.facebook.com/method/photos.addTag?pid='.$pid.'&tag_uid='.$userId.'&x=50&y=50&access_token='.$this->facebook->getAccessToken();
$ch = curl_init();
$url = $json;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_GET, true);
$data = curl_exec($ch);
And this line is very important:
curl_setopt($ch, CURLOPT_GET, true);
you must youse CUROPT_GET instead of CUROPT_POST to add a Tag throw the REST API.
I Hope this helps you.
Best wishes Kay from Stuttart
Photo id is unique for every user and looks like two numbers joined by underscore in the middle.
Getting this id is a bit tricky.
You can get it by running FQL on photo table but you need to provide album id which is also user unique. You can get album id from album table but you need to provide owner userid.
For example we have CocaCola user with userid 40796308305. To get photo ids from this user we can run FQL:
SELECT pid FROM photo WHERE aid IN ( SELECT aid FROM album WHERE owner="40796308305")
(you can run it in a test console on this page)
This would return our photo ids:
[
{
"pid": "40796308305_2298049"
},
{
"pid": "40796308305_1504673"
},
{
"pid": "40796308305_2011591"
},
...
]
I didn't work with photos much, maybe you don't have to go through all this process to get photo id, it might be some simple algorithm like <OWNER_ID>_<PHOTO_ID>. But try to get your photo id from FQL and see if tagging would work. If it does maybe you will be able to skip FQL part and build photo id from existing data you have.
Hopefully this helps.
I found out the problem, the problem is that when you use curl to send the array the post function will not send the array correctly, it was sending just "Array" that it was why the graph API complained about tags being an array. To solve that I did the following:
$data = array(array('tag_uid' => $taguser,
'x' => rand() % 100,
'y' => rand() % 100
));
$data = json_encode($data);
$photo_details = array(
'message'=> $fnames,
'tags' => $data
);
Now I just send using curl
curl_setopt($ch, CURLOPT_POSTFIELDS, $photo_details);