I am developing a php script for retrieving messages from a page (not user) in facebook. I have access to that page (it is mine) so if I must to modify something in config, I can.
I tried the FQL query in graph api webpage from Facebook and it works. I get the file with all messages.
But whenever I try it in my php script, it fails. I am trying it in a localhost server and calling the script : "localhost/testServer/phpScript.php" I don't know if I have to add a parameter.
I have checked many examples and tutorials and all are similar but in my case, they don't work.
I always get an exception with this message:
Fatal error: Uncaught Exception: 100: Requires user session thrown in C:\xampp\htdocs\testServer\base_facebook.php on line 1271
If I surround it in try/catch and print what I get, it tells it is an exception with getType() and an array with getResult().
My code is quite simple:
<?php
require_once("facebook.php");
//Get Facebook SDK Object
$config = array(
'appId' => '*******',
'secret' => '*******',
'cookie' => true,
);
$facebook = new Facebook($config);
//$fql = "SELECT message FROM status WHERE uid = 169070991963 ORDER BY time DESC";
$fql = "SELECT message FROM status WHERE uid = 169070991963";
//Create Query
$param = array(
'method' => 'fql.query',
'query' => $fql,
'access_token'=>'*******|******-*******',
'ext_perm' => "read_stream",
);
$result = $facebook->api($param);
print_r($result);
?>
I just deleted my appId, secret and access_token.
I read something about read_stream permissions using status table. But adding access_token and ext_perm tags has no effect. I leave the uid of the webpage because it is public so you can test the code on your own.
If I can't do this query with php fql, I have no problem in using normal api if I can get the same result.
I am expending many days doing this script and I am completely lost with the problem.
Related
I am struggling with one problem.
I have to send notification to Facebook Users. According to >=2.0 version of Graph API, we get app scoped ids of user instead of original UIDs.
My question is: can we send notification to user using these new app scoped ids?
As i tried to send notification with app scoped ids, code failed. But When I tried same code with original user id, it worked.
Please let me know if there is any way to solve this problem.
Thanks in Advance.
Here is my code:
require_once "/facebook_api/facebook.php";
$facebook = new Facebook();
$app_id = '<app_id>';
$app_secret = '<secret_id>';
$app_access_token = $app_id . '|' . $app_secret;
$id = '<app-scoped-id>'
$response = $facebook->api( '/'. $id .'/notifications', 'POST', array(
'template' => 'You have received a new message.',
'href' => 'http://test.com',
'access_token' => $app_access_token
));
print_r($response);
die;
RESPONSE:
If is use app-scoped user id, response is:
PHP Warning: Missing argument 1 for Facebook::__construct(), called in /facebook/message.php on line 4 and defined in /facebook/facebook_api/facebook.php on line 47
PHP Notice: Undefined variable: config in /facebook_api/facebook.php on line 51
PHP Fatal error: Uncaught GraphMethodException: Unsupported post request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api
thrown in /facebook/facebook_api/base_facebook.php on line 1283
if i use original user id, result is: Array(['success']=>1);
Currently you can use both the global ID and app scoped ID in your app. Facebook seems to have relaxed this restriction.
If your app scoped ID isn't working, file a bug.
I'm doing the same thing and it also works with real userID, but when I use app scoped id, I get this error :
(#803) Some of the aliases you requested do not exist: ...
My facebook app has all of a sudden stopped working, I run this FQL once but if i try to run it twice it comes back with the error:
Fatal error: Uncaught Exception: Requires user session thrown in
here's my function:
function get_top_fb_friends()
{
require_once 'src/facebook.php';
require('facebook_api_connect.php');
$accessToken = $facebook->getAccessToken();
$params = array('method' => 'fql.query',
'query' => 'SELECT uid, pic_square, name
FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
ORDER BY mutual_friend_count DESC','access_token'=>$accessToken);
$friend_array = $facebook->api($params);
return $friend_array;
}
I tried adding in the access token but it still fails the second time.
it seems to be the same problem as described here:
FQL Error 102 Requires user session on a repeated query
im at a loss as to how to get it to work!
If you don't implement Facebook Login, the Access Token you'll receive from
$accessToken = $facebook->getAccessToken();
is very likely an App Access Token, which is not valid for requesting the kind of information you want. Also, you need the user_friends permission, and apparantly you're using an old version of the SDK, because 'method' => 'fql.query' is outdated.
See
https://developers.facebook.com/docs/facebook-login/access-tokens
https://developers.facebook.com/docs/apps/review/login#do-you-need-review
https://developers.facebook.com/docs/technical-guides/fql/#read
I have to post on a users wall when he is offline. I went through number of posting and checked the code written below seems ok and also not throwing error and complete successful yet I am unable to see posting on users wall. Pl help.
$facebook = new Facebook(
array(
'appId' => "446262598768110",
'secret' => "<REMOVED>",
)
);
$facebook->setAccessToken("<REMOVED>");
//create message with token gained before
echo " lets try posting";
$access_token = $facebook->getAccessToken();
$user_id = $facebook->getUser();
echo "\ndone3..... $access_token.-----.$user_id.----new\n";
$post = array(
'message' => 'This message is posted with access token - ' . date('Y-m-d H:i:s')
);
//and make the request
$res = $facebook->api("/me/feed", 'GET', $post); //have tried both Post and Get method but result remain same
offline access was removed quite a few months ago. also, you do have to "POST" to facebook graph, everytime you want to publish something.
without seeing the result of the facebook graph, it's kind of impossible to help you at all.
nevertheless: double check, that you're using the correct access token. you have to extend the short lived access tokens by your own (https://developers.facebook.com/docs/howtos/login/extending-tokens/), so that the access token itself is still valid when you want to post by cron-job.
if you're trying to post by using the app access token, you can't use "/me/feed" at all.
I have a fully functional FQL query, but when it is triggered for the second time i get an error code 102: Requires user session
On my app i have an Autocomplete Search for Friends.
I am using jquery ui autocomplete with a dynamic source using AJAX.
This is my PHP Function:
function fb_buscarAmigosAutocomplete($term='',$app_user=false){
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret'=> APP_SECRET
));
$term=trim(strtolower($term));
//Buscar usuarios
try{
$fql = " SELECT uid,name,first_name,last_name,pic_square
FROM user
WHERE uid IN (
SELECT uid2
FROM friend
WHERE uid1=me()
)
AND strpos(lower(name),'".$term."') >=0
";
if($app_user)
$fql .= " AND is_app_user";
$fql .= " LIMIT 5";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$result = $facebook->api($param);
} catch(FacebookApiException $e){
$error=$e->getMessage();
var_dump($e);
}
if(!empty($result)) {
$return['amigos']=$result;
$return['valido']=true;
}else
$return['valido']=false;
return $return;
}
The weird thing is that this was working fine for the last 2-3 weeks, and all of a sudden it stopped. Even "weirder", it still works the first time it is triggered, but not the second time. Since this is asynchronous, i dont understand what difference does it make if is the first, second, third, fourth time it is triggered..
Any ideas?
Edit 1
With further research i think i got a workaround.
This may be because a bug reported here ans discused here
My workaround was to catch the first access_token i get with the Object Facebook, then attach it to every single instance of the Class, something like this
First connection:
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret'=> APP_SECRET
));
$uid = $facebook->getUser();
if($uid){
$_SESSION['access_token']=$facebook->getAccessToken();
}
Any other use of the api:
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret'=> APP_SECRET
));
$facebook->setAccessToken($_SESSION['access_token']);
try{
$user_info = $facebook->api('/'.$uid.'?fields=id,name,first_name,last_name,email,picture');
} catch(FacebookApiException $e){
$error = $e->getMessage();
}
So far it has solved the problem
Edit 2
it did not... sometimes my access token is a valid alphanumeric +100 characters String and those times it works.
Sometimes it is a 48 only numbers String, and it doesn't... i'm really stucked here
The reason this is likely happening is because of the December 5, 2012 "Breaking Changes" that Facebook has rolled out. Specifically:
New security restrictions for OAuth authorization codes
We will only allow authorization codes to be exchanged for access tokens once and will require that they be exchanged for an access token within 10 minutes of their creation. This is in line with the OAuth 2.0 Spec which from the start has stated that "authorization codes MUST be short lived and single use". For more information, check out our Authentication documentation.
This is why your first request works, your second doesn't. On the 2nd attempt, the SDK attempts to exchange your the user's auth code for an access token. But due to these changes, this can only be done once. Your code looks like it should be working properly, since you are explicitly saving and then setting the access_token after you first receive it. Make sure that that is the case (confirm that the $_SESSION var is being saved/retrieved properly). If for some reason it is not, the 102 error is what you will see.
Cheers
I am trying to create an album for a page, however I get the following error;
[15-Sep-2011 22:38:14] PHP Fatal error: Uncaught OAuthException: (#100) Invalid ID for album owner
thrown in C:\inetpub\wwwroot\Diveengine\v3\facebook\base_facebook.php on line 988
the code is as follows
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => "aaaaaaaaaaaa",
'secret' => "aaaaaaaaaaaaaaaaaaaaa",
));
$facebook->setAccessToken("aaaaaaaaaaaaaaaaaaaaa");
$me = $facebook->api('/me');
//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'message'=> 'test album',
'name'=> 'Test Album'
);
$create_album = $facebook->api('/pageid/albums', 'post', $album_details);
// I have the page ID
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
I have been able to create an album for my profile, however I want this for the page.
ok dude, it took long enough to figure this out, but here goes. Your code is probably fine, but the trick is getting the right access token. Originally, I thought I had to create a page access token (using the graph api explorer) with all the frills that would allow me to do what I wanted.
Turns out, all you need to do is this:
1) go to https://developers.facebook.com/tools/explorer
2) it'll probably auto populate your facebook id, but affix /accounts at the end there so it'll look like this:
#yourfacebookidnumber/accounts
or you can just change it to
https://graph.facebook.com/me/accounts
you'll get a JSON dump of a list of all the fan pages you're an administrator to. Find the fan page you want to post to (you need to be an administrator of that fan page).
Grab the access token specified there, use it in your code, and Bob's your uncle!