Simple Facebook status return using FQL - php

I'm trying to create a box on the side of a page that displays the 6 latest posts from a Twitter account and Facebook page. Twitter is working exactly how I want it to - Facebook is not.
The way I am doing it at the moment is to fetch the latest statuses using FQL as JSON usin cURL, caching them on the server, then displaying them in the same way as I am the tweets in the actual box.
My problem lies with the access token. I went away last night with it working, came back this morning and noticed that the modified date of my cached file hadn't changed. I checked the cached error file (where the fetched JSON gets dumped if it doesn't contain the data I need) and I had a session expired error. I then created a small function that uses cURL (3 cURL calls or one box, I know - urgh) to retrieve a new access token using the this URL:
https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=XXX&client_secret=XXXX
But I now get an error that a User Session is required, but all I want to do is to read the latest posts, that isn't user dependant at all. I really want to avoid using the Facebook SDK if possible.
Tl;dr - FQL seems to require users to log in even just to read statuses off of a set page's wall - what's the correct way of getting this data that doesn't require a user session (preferably without using the SDK).

If you're trying to get the status from a User account then you will require a valid user access token.
If you're getting statuses from a Fan Page then you just require a valid app access token - which can be obtained from:
https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials
You can then use the returned access token to get the statuses from the target Fan Page.
SELECT id,status_id,message,time FROM status WHERE uid = XXXX LIMIT 6
You're querying by user id so that will require a valid user access token.

Related

How to get like count of an FB page in graph sdk (PHP)?

I've recently been working with the graph API for Facebook and i want to create a script that lets me input a page_id and will then return the like count of the specified page.
I've already taken care of getting an access token and logging in, but i can't seen to find anything concerning the total like count of a page in the Reference.
This would be the API call:
https://graph.facebook.com/bladauhu?fields=fan_count&access_token=xxx
You can use any Token if the Page is not restricted by age or location. If it is restricted, you need a User or Page Token of a user who can see the Page.
More information:
http://www.devils-heaven.com/facebook-access-tokens/
https://developers.facebook.com/docs/facebook-login/access-tokens/

Facebook Open-Graph API Subscription or Update Notification without App Permission

I'm relatively certain after reading the facebook Open Graph documentation that I can't have a web site 'subscripe' to a public page, unless that page installs my app. Let me know if that is wrong.
What I am trying to make is a photo gallery, pretty simple, but it grabs photos from a public 'page' such as a college or university.
I would like it to update itself anytime there is a change. I was hoping I could do it through the API, possibly using API updates/subscription
Facebook App Subscriptions -
but I don't think that will work. I'm using PHP, are there other ways to listen and see if there has been a change? Other than just firing off a function every now and then, using CRON or some other such server tool. I would think that facebook would probably also not like that behavior, though I'm not sure.
As of right now, I am able to grab JSON from a public page's photo gallery using the API and the photo gallery works just fine, I'm mostly just wondering what the optimal of having it update itself automatically is.
You are correct in assuming that real-time updates for pages require admin access to the page to work. You need a page access token to be able to subscribe to updates for a specific page, and such a page access token you can only get from users that have admin privileges for that page. (The page does not need to install your app as a tab any more, they changed the subscription process in that regard; but the page access token is still necessary. Basically you can say, you can not subscribe to updates for just any arbitrary page, it has to be “your” page.)
Apart from that, all you can do is check for new content by requesting the current data in a sensible interval.
you need to get page access token, which is granted when the user is one of the Page Admin, check the documentation on Facebook
https://developers.facebook.com/docs/graph-api/reference/page
look for this line
"access_token": "The access token you can use to act as the Page. Only visible to Page Admins"

Get Facebook User's real id using App Scoped Id

I know this question already has been discussed, But I'm here with a different way and a confusion.
First : if I do debugging in Graph API Explorer it returns different app_scoped_user_id and it return me correct json when I run graph.facebook.com/$app_scoped_user_id but with my app it generate different App Scoped ID which don't works with above method. Why that happen?
Second : I thought to get last redirect url using CURL but it returns http://www.facebook.com/login.php?next=http://www.facebook.com/app_scoped_user_id/$app_scoped_user_id . Now I've two thoughts (may be stupid thoughts),
Can I open that app scoped url in a popup window and get that window url after redirection and close the popup?
Can I login in Facebook anyhow using CURL so that i can get real user_id?
I know, app scoped user id can be used. But I want to give it a try.
Warning: Facebook old API in use
There's a way to get it, but you won't like it:
get the user to log in to your app via Facebook
make a second call to the API using the retrieved access token to get their username
https://graph.facebook.com/me?access_token={THE_ACCESS_TOKEN}
make a third call to that username without an access token to get their Facebook id
https://graph.facebook/{USERNAME} <= No access token here!
EDIT: sorry, old API is used here.

Generating a monthly "Post Count" from Facebook API

I am trying to figure out how I can query the FB Graph API (or FQL API) to be able to get number of posts (or status-updates) for a given period of time. I am able to generate an access token using
$access_token = $facebook->getAccessToken();
So that would lead me to believe that the PHP SDK is installed correctly and working. When I try to use the access token in a fql query it fails giving an error message of: A user access token is required to request this resource. The query I'm trying is just to return the current status_id. I tried using uid=me() as well as using an actual user id (testing id from the API). Here is the query url:
https://graph.facebook.com/fql?q=SELECT+status_id+FROM+status+WHERE+uid=me()&access_token=
I'm not sure where I'm going off track with this, so any help would be greatly appreciated.
Thank you for looking.
This is your app id and secret, please do not ever post this to public, anyone will have full control of your application. Reset your token immediately
The PHP SDK defaults to the application access token (which should never be posted in public). You need to have the user login with getLoginUrl() an example is given at
https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php

How do I access wall posts made by the user in a facebook app

i am having trouble accessing the wall of a user. I am making a request to 'https://graph.facebook.com/'.$userID.'me/feed?access_token='.$access_token
where $access_token contains my access_token.
the strange part is that it is working well when I make the request from the application page... that is: http://apps.facebook.com/myapplication/ but when i make the same request in my callback script, it returns only those posts which are made by some application, and it does not show user's own posts like, status messages etc.
Try using the new PHP SDK, i've seen people solving bugs like yours just by changing to the SDK. It usually doesn't require you to pass the access token and supports all methods of the API (i think).
With /me/feed you can access the feed of the currently logged in user. To get the feed of some other user, you need to use their id. I am assuming that's the reason why its working in the application page and not in the script.

Categories