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
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: ...
I'm working on an iPhone App API written with Phalcon Framwework and Facebook PHPSDK v2. All of the authentication is done on the client and once the fb access token is verified by the server a separate HMAC auth is set up between the client and server.
My end goal is to return a list of friends ids, names and profile pic urls, who have authorised an app given a new users users fbid or acccess token. I have used this library to include the SDK in my Phalcon application https://github.com/geass/Phalcon-Facebook-Library
I've already scoured SO for an anwer and tried a few graph requests but they don't seem to work.
I have used the following FQL found readily on SO in the graph API explorer:
SELECT uid, name FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = 123456) AND is_app_user = 1
which seems to work, if the access token is valid.
when i try to use the same FQL in the SKD im not sure how I can apply the access token, simply doing:
function getFriendsUsingApp($fbId){
$params = array(
'method' => 'fql.query',
//'query' => "select pic_square from user where uid=$fbId",
'query' => "SELECT uid, name FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = $fbId) AND is_app_user = 1",
);
$result = $this->_facebook->api($params);
return($result);
}
This result in the following error:
Fatal error: Uncaught Exception: 102: Requires user session
thrown in
D:\library\Social\sdk\base_facebook.php on line
1271
How can I get this information given either the fb id or Access token or both?
Thanks.
The solution i found was to invest the time in getting the FBSDK to run properly in Phalcon and then use a graph request (which neatly deals with a lot of issues i was having with using HTTPS in file_get_contents()). Somenone else then pointed that out that the FB API docs i say you can just add the access token on the end of a GET graph request.
In my facebook library file:
function getFriendsUsingAppIds($fbId, $accessToken){
return $this->_facebook->api("$fbId/friends?fields=installed&access_token==$accessToken");
}
This gave
[0] => Array
(
[id] => 305232
)
[1] => Array
(
[id] => 305391
)
[633] => Array
(
[installed] => 1
[id] => 100005007310527
)
I completely missed row 633 at first and assumed i was just getting a list of all friends.
I then removed the rows which didnt have installed set with a simple foreach.
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.
i'm trying to write a script to post to a page while the admin is offline.
my application has the manage_pages extended permission of the admin user.
here is my code:
require('php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'MY_APP_ID', // YOUR APP ID
'secret' => 'MY_SECRET', // YOUR API SECRET
'cookie' => true
));
$user_admin_id = 'MY_ADMIN_ID';
$page_id = 'MY_PAGE_ID';
//get the access token to post to my page via the graph api
$accounts = $facebook->api("/" . $user_admin_id . "/accounts");
foreach ($accounts['data'] as $account)
{
if ($account['id'] == $page_id)
{
//found the access token, now we can break out of the loop
$page_access_token = $account['access_token'];
break;
}
}
but I always get this message:
"Fatal error: Uncaught OAuthException: A user access token is required
to request this resource. thrown in
/home/itrade10/public_html/khodiersoftware/php-sdk/src/base_facebook.php
on line 1033"
You forgot to authorize the User, that´s how you get a User Access Token:
https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/
Don´t forget to add the "manage_pages" permission in the scope Parameter. You will also have to use the Function "setExtendedAccessToken" of the PHP SDK to extend the User Token. After that, you will get a Page Access Token that is valid forever with the /me/accounts endpoint.
If you used getLoginUrl already, then there´s something wrong with that code, you may want to add it to the question.
Before getting the accounts (with /me/accounts, not with your id), get the User ID:
$user = $facebook->getUser();
If you got a valid User Token, your ID will be in the $user Variable.
maybe this one? https://developers.facebook.com/roadmap/offline-access-removal/ also see Getting long-lived access token with setExtendedAccessToken() returns short lived token
The error message is telling you exactly what the problem is... you need to get a user access token to access your page tokens
I have been trying for several days now to successfully post to a client's Facebook Page wall from their website. I need to be able to do this without having a user logged in, which I was able to achieve with an extended access token generated by going to the URL provided in the docs.
What I am trying to do is to fetch an extended token using the PHP SDK as in this question - Facebook PHP SDK: getting "long-lived" access token now that "offline_access" is deprecated, however I receive the following error:
(#200) The user hasn't authorized the application to perform this action
The debug of the auth token generated by the Graph API Explorer shows myself as the User and includes the needed manage_pages and status_update scopes. However, if I run me/accounts, the perms array does not list these under the data for the page in question - not sure if this is relevant.
Here is the code that I have attempted to use:
$facebook = new Facebook(array('appId' => 'xxxxxxx', 'secret' => 'xxxxxx'));
$pageID = 'xxxxxxxx';
$facebook->setExtendedAccessToken();
$accessToken = $facebook->getAccessToken();
try {
$page_info = $facebook->api("/$pageID?fields=access_token");
print_r ($page_info);
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
if( !empty($accessToken) ) {
$args = array(
'access_token' => $accessToken,
'message' => "Catch phrase!"
);
$facebook->api("/$pageID/feed","post",$args);
} else {
// Handle error
}
UPDATE
I found that if I echoed the output of getAccessToken() the result was the app ID and the secret separated by an | - is this what I should be receiving? Seems odd to me since all of the other tokens I have seen are random and much longer.
ANY help at all would be appreciated, I have wasted so much time on this so far. It just seems to work for everyone else.
UPDATE 2
Hi Warren! Looks like you're new around these parts, welcome! Thanks for your research into this, do I need to save these tokens into a DB table and check to see if they are expired every time I try to post? I am ONLY posting to a Page as the Page, apparently this requires the Page access_token rather than what I am assuming is the USER access_token. I believe I also read somewhere that the Page access token never expires, although it changes if I refresh the Graph API Explorer page or request a new user token. Very puzzling.. do not know if I even need to deal with getting new tokens in this app or just use a Page access_token that appears to work?
Also just looking at the base_facebook.php file and the following function:
/**
* Returns the access token that should be used for logged out
* users when no authorization code is available.
*
* #return string The application access token, useful for gathering
* public information about users and applications.
*/
protected function getApplicationAccessToken() {
return $this->appId.'|'.$this->appSecret;
}
Shows what you are experiencing the function setExtendedAccessToken() calls getAccessToken() which calls the function above to do the following inside setExtendedAccessToken:
$access_token_response = $this->_oauthRequest(
$this->getUrl('graph', '/oauth/access_token'),
$params = array(
'client_id' => $this->getAppId(),
'client_secret' => $this->getAppSecret(),
'grant_type' => 'fb_exchange_token',
'fb_exchange_token' => $this->getAccessToken(),
)
);
the return of this function is then {"error":{"message":"No user access token specified","type":"OAuthException","code":1}} but because there is no access_token the function just returns false
the fb_exchange_token can not be the app id and app secret combined.
I have done some testing and what I have found out is if you get the user to login, you can get there access token. If you pass this token into setAccessToken then run setExtendedToken. If you then save the toke by running getAccessToken you can use this token later when the user is logged out.
$facebook->setAccessToken($facebook->getAccessToken());
$facebook->setExtendedAccessToken();
$access_token = $facebook->getAccessToken(); // save this for later use
You need to get the user to login to ask them for permission to get access to there account. If you need to get access to a facebook page make sure they are an admin and ask for the manage_page permission.
Hope this helps
From what I have found so far the extended token that is returned when the user is logged in does not expire as you do not get back a unix timestamp so you would have no way of knowing when it expires.
This is what I have done in my application.
Request the user to login to facebook and except the access permissions. Use getAccessToken to get the current access token. With this I pass into getExtendedAccessToken then run getAccessToken again to finally get the extended access token which I store in a database for later use.
$facebook = new Facebook(array(
'appId' => 'YOURAPPID',
'secret' => 'YOURSECRET',
));
$params = array(
'scope'=>'publish_stream,manage_pages'
);
$loginUrl = $facebook->getLoginUrl($params); // Use this to request them to login in then when they are do the following you will need them to return to your app
$facebook = new Facebook(array(
'appId' => 'YOURAPPID',
'secret' => 'YOURSECRET',
));
$facebook->setAccessToken($facebook->getAccessToken());
$facebook->setExtendedAccessToken();
$access_token = $facebook->getAccessToken(); // store $access_token for later use