Retrieving Facebook Events using FQL (permissions issue) - facebook

I'm trying to retrieve all facebook events that a page that I have created, has been invited to. I created an App and following is the code I got from http://www.codeofaninja.com/2011/07/display-facebook-events-to-your-website.html:
//requiring FB PHP SDK
require 'fb-sdk/src/facebook.php';
//initializing keys
$facebook = new Facebook(array(
'appId' => '[app_id>],
'secret' => '[app_secret]',
'cookie' => true, // enable optional cookie support
));
$fql = "SELECT name, pic, start_time, end_time, location, description
FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = 100001660923071 )
ORDER BY start_time asc";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
print_r($fqlResult);
Now the problem is that it works fine when the uid in the query is set to '221167777906963'(its the UID from the example code and it works wine with any app-id/secret combination) but when I set it to the uid of the page I am the owner of none of the events get returned. Is there something I'm missing? I double checked on my pages>accounts settings>apps to see if the app that I created had permission to the page and it does. Its a public page. What am I missing in the flow to give this app permission to get access to my pages events? The same query works fine if i run it in the Graph API Explorer with an access token that I generate for user_events.
I'm basically trying to understand why a particular Wordpress Plugin called FB Sync Events isn't working and the app authentication is what seems to be failing cuz the $facebook->api is returning nothing for a fql.query JUST for my page. QQ

For accessing the page's events, you might need to use a PAGE access token. Page access tokens are easy to get. For the page administrator's user access token, call Graph /me/accounts and in there will by the page along with an access token for that page. That is what is called a "Page Access Token". Then using that Page access token, call your FQL statement.

As far as I know it's no possible to display the events you have been invited to if you create a Facebook page instead of a profile. Since when you create a page you only have users who like you page. This means they can interact with the content you publish but they can't interact with you as a brand or company.
On the other hand when you create a profile you have users who are your friends and they can interact with you and the content you publish in many ways, including being able to be invited to events.

Related

how to post facebook user wall/ page status by a single user only no app install for other users

I am working on a site and i need to post page links from that site to a special user wall or page if something is published on it which means i only need one user to post that question.
the problem which i am facing is access token since i am not trying to show facebook login page in front of website traffic. its not like sharing on user wall we are basically trying to post status on a page automatically so any help for this problem will be appreciated.
i am using the following code which seems to work fine as long as the access token for the page is valid but i need to make something permanent so that i can add all the info in the php code hardcoded and it work for some weeks at-least without changing the api key or token
$appid = 'code';
$appsecret = 'code';
$wall= $pageId = 'code';
$token='page token';
$facebook = new Facebook(array('appId' => $appid, 'secret' => $appsecret, 'cookie' => true));
$params=array( 'message' => $msg,'name' => $title, 'caption' => $title, 'link' =>$uri,'description' => $desc, 'picture' => $pic, 'access_token' => $token,'actions' => json_encode(array('name' => $action_name,'link' => $action_link)) );
$result = $facebook->api($wall.'/feed/','post',$params);
if i make token from the graph api debug page then it works fine but after some time i got the message "OAuthException: An active access token must be used to query information about the current user."
i am really desperate to find something solid for it.
thank you
You can write a backend script (maybe a cron job using the FB PHP-SDK) that runs as the special user and makes the desired FB api calls. For this, you will want to use a long-lived access token, which needs to be renewed every 60 days. You might also try your FB api calls using an App Access Token, which do not expire but also do not support all FB publishing and other operations.
To post to someones wall (or perform any action with the open graph) you must be authenticated with facebook as somebody in their graph or an application that they have granted permission to.
You should look at the permissions that can be set using the oauth scope attribute...
https://developers.facebook.com/docs/reference/dialogs/oauth/
To me it looks like they will need to grant your application publish_stream if you want to write to their friends walls as well as the user that you are authenticated as.
https://developers.facebook.com/docs/reference/login/extended-permissions/

How to read stream from certain user with app access token

I created facebook app that is requesting aditional permissions read_stream and publish_stream.
After user accepts app and allows app to read posts when I try to get posts with fql query I get empty results.
Here is code
$appAccessToken = APP_ID|APP_SECRET
$fql = 'SELECT post_id, type, likes, source_id, created_time, privacy, comments FROM stream WHERE source_id = xxxxxxxxxxxxx';
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => '',
"access_token" => $appAccessToken
);
$fqlResult = $facebook->api($param);
and results are empty array
Array
(
)
I can get results for active user using user_access_token ("access_token" = $facebook->getAccessToken();) or even without access token in fql query.
The problem is that I need access to all user posts that are using app not only for active user. What would be the best way to read stream for offline users?
You could request a long lived access token
https://developers.facebook.com/roadmap/offline-access-removal/
but you cannot use an app access token to read a private user stream. You can only use a users access_token to read their stream as it is a user permission and so requires their access token to validate they have authenticated your app with that permission. In fact if you look at the docs for the stream table, it mentions you can only read it for the current session user (it's the sentence above the 1st 2 bullet points)
https://developers.facebook.com/docs/reference/fql/stream/
An app access token should be able to read public posts by a user though.
I'm not sure what the implications are on storing the long lived access token so you will have to do your research to make sure you don't violate any TOS.

Facebook Graph api How to get page ID via an access token

For "users" on Facebook, I gather information like their user ID by using this graph api call:
https://graph.facebook.com/me?access_token=...
This works every time.
My question is... is there a way to get the page information about the page that was just authorized via the access token that is returned after authorization?
I tried this, and id doesn't work, but this is what I'm looking for
https://graph.facebook.com/page?access_token=...
As you know, in order to post to a "page" wall is to 'Post' like so...
https://graph.facebook.com/PAGEID/feed?message=blahblah&access_token=XXXXXXXXXXXXX
How do I get the page ID and other info about the page that was authorized without already knowing the ID or page "username" (which you have to have 25 likes to use anyway)???
Thank you for any help stackoverflow community:)
The answer I was looking for is here. You can simply use a users access_token to get a set of their page_access_tokens, and give them the option of posting to these pages using these access tokens based on the return JSON object data.
pages are not authorized, applications are.
in case that your application has permissions, the current page is informed thru the signed_request that FB passes to the app canvas. use the php sdk to read it
https://developers.facebook.com/docs/authentication/signed_request/
There's no callback when an application is installed on a page. However, if a user is using your application via a tab iFrame you can get that pages id from the signed_request. You could maintain this information as part of your own user session, and use it as you see fit.
The contents of a signed_request to a tab iFrame would be similar to the following (output of PHP print_r):
stdClass Object
(
[algorithm] => HMAC-SHA256
[issued_at] => xxxxxxxxxx
[page] => stdClass Object
(
[id] => FAN_PAGE_ID // target page id
[liked] => 1 // is the user a fan
[admin] => // is the user an admin
)
[user] => stdClass Object
(
[country] => ie
[locale] => en_GB
[age] => stdClass Object
(
[min] => 21
)
)
)
For more information see:
http://developers.facebook.com/docs/authentication/signed_request/
You may also want to take a look at the manage_pages permission
Also, see here:
http://developers.facebook.com/docs/reference/api/
Page Login
You can impersonate pages administrated by your users by requesting the manage_pages permission.
Once a user has granted your application the "manage_pages" permission, the "accounts" connection will yield an access_token property for every page administrated by the current user. These access_tokens can be used to make calls on behalf of a page. The permissions granted by a user to your application will now also be applicable to their pages.

Facebook Graph API problem

I've got some trouble with Facebook authentication. Even when I'm logged, the function getUser() returns 0
Here's my code :
$fb_params = array(
'appId' => APP_ID,
'secret' => SECRET_ID
);
$fb = new Facebook($fb_params);
echo $fb->getUser(); // UID
Someone's got an idea?
PS : 'I can no long access to $fb->api('/me'), it says it requires an access_token, I think it's linked to the authentication issue...'
Thanks
You are currently not authenticating as a user, only as an application. As a result, the Facebook API can't show you the /me page or respond to a getUser() call since it doesn't know what user you are trying to access the API on behalf of (ie. "Who is /me?"). You will also only be able to access publically-accessible information.
You need to get a user to authenticate your application through Oauth2, store the access_token you are returned, and then include it in any future calls (eg. WIRQjCey1.3600.1309525200.0-509450630|eD6SAR">https://graph.facebook.com/me?access_token=2227470867|2.AQB-_WIRQjCey1.3600.1309525200.0-509450630|eD6SAR...).
To do this using the PHP SDK you can do
$loginUrl = $fb->getLoginUrl();
echo "<a href='$loginUrl'>Login with Facebook</a>";
Clicking that link and having the user authenticate will store the access_token to the $_SESSION, and when you hit refresh the "new Facebook( $fb_params );" constructor will pick out the access token from the $_SESSION and use it for all future calls, so then calls like $fb->getUser(); will return correctly.
There's a functioning example in the examples folder of the SDK, here:
https://github.com/facebook/php-sdk.
You can use it to try calls while authenticated as an application (public data access only), and then as a user.

Facebook friends online

The question is:
need a php script put on cron - it will mail me facebook user friends status (online or not). Found two variants:
1. using fql query $result = $facebook->api(array(
'query' => "SELECT name,online_presence FROM user WHERE uid IN (
SELECT uid2 FROM friend WHERE uid1 = me())",
'method' => 'fql.query'));
2. using xmpp. Logging to jabber facebook chat and get user presence from it.
But the question is - who to make it using only users facebook login and password (without secret facebook key, application id).
Just define the user name, password and the script will fetch the friends status.
The only way to do what you're trying to do without using the API (FQL or Graph) or XMPP (Jabber) would be to try and act like a browser to scrape facebook pages (which technically is against their TOS).