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.
Related
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.
I have an app in which I want to check my users' online presence. Right now, when I check, it tells me everyone is offline.
The users grant user_online_presence and offline_access (plus a few other) permissions to the app. Then I store the access token. I am able to access any information I need at any time except for the online presence.
To get the online presence, I first set the access token:
$facebook->setAccessToken($user['AccessToken']);
Then I query Facebook:
$fql = "SELECT uid, name, online_presence, status FROM user WHERE uid = ".$user['FacebookID'];
$param = array('method' => 'fql.query', 'query' => $fql, 'callback' => '');
$fqlResult = $facebook->api($param);
When I check the results, I get the id, name, and status, but everyone is listed as offline, even me when I know I'm online and active:
[online_presence] => offline
Any help getting this working would be greatly appreciated. Thank you all for your time.
I used this FQL query sucessfully :-
SELECT name,online_presence FROM user WHERE uid
IN (SELECT uid2 FROM friend WHERE uid1 = me())
to get all the online friends. Got output as "idle","active" and "offline". All values of which were absolutely correct.
There are 2 permits
user_online_presence, for the user
friends_online_presence, for friends
http://developers.facebook.com/docs/reference/api/permissions/
online_presence is for chat online, not facebook online
The user's Facebook Chat status. Returns a string, one of active,
idle, offline, or error (when Facebook can't determine presence
information on the server side). The query does not return the user's
Facebook Chat status when that information is restricted for privacy
reasons.
Also, I ran fql?q=SELECT name, online_presence FROM user where uid IN (Select uid1 from friend WHERE uid2=me()) and it listed my friend's online status fairly accurately. Only a very few were incorrect. Maybe that's due to a lag in the facebook update system, or maybe they're caching the information for too long, etc.
In my app a user grants my application extended permission and I get the access token and store it as its a long lived access token.
This user is shown a list of their friends and they select one.
I store their name and fbuid in my database.
Now on a day in the future I want to send a message to this friend's wall.
Is this possible?
If not what other options are there to send a link to the friend in Facebook when the initial user is offline?
* Update
I was able to do this with the long lived access token and offline access as follows:
access_token = "AAACvmqy1nYoBAAZCk*************dZAMjsLxKxR7DaZBE0NxY8ZBGBW1q2mzsB9TDT0RvgeQcDdnyFJNAYRf0icnhlbikZD"
appID = '19307***********'
message = "Happy Birthday from your friends."
name = "Click here for your Birthday Surprise"
redirect_uri = URI.parse('http://localhost:3000/facebook/')
link = "https://www.facebook.com/dialog/oauth?client_id=#{appID}&redirect_uri=#{redirect_uri}"
caption = "Your friends have created a surprise for you. Click the link to see what it is."
picture = 'http://www.birthdaywall.net/logo-mail.png'
userID = '100002*********'
uri = URI.parse("https://graph.facebook.com/#{userID}/feed")
req = Net::HTTP::Post.new(uri.path)
result = req.set_form_data({:access_token => access_token , :message => message, :app_id => appID, :name => name, :link => link, :caption => caption, :picture => picture, })
sock = Net::HTTP.new(uri.host, 443)
sock.use_ssl = true
sock.start do |http|
response = http.request(req)
end
I don't know if this is the best way but it works for me so I am using it. If anyone has a better way I would be very happy to know about it.
I guess you have the publish_stream permission already. You also need the offline_access extended permission to make your access token live long:
Enables your app to perform authorized requests on behalf of the user at any time. By default, most access tokens expire after a short time period to ensure applications only make requests on behalf of the user when they are actively using the application. This permission makes the access token returned by our OAuth endpoint long-lived.
See here: https://developers.facebook.com/docs/reference/api/permissions/
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.
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).