Script that grabs a Facebook like page's latest status? - facebook

I am trying to write a script that grabs the latest Facebook status. I get the latest status by calling https://graph.facebook.com/LIKEPAGE?fields=statuses.limit%281%29.fields%28message,updated_time%29&access_token=TOKEN
The problem is that the token expires. I read someting on Facebook about the problem. https://developers.facebook.com/roadmap/offline-access-removal/
Can a server access the page (graph.facebook) and fetch the JSON-response? Or do I have to make the user login everytime? That is an option, but I would like to avoid it if it is possible?
So, can I fetch the latest status via a PHP script without makeing the user login?

You should dynamically generate the access token. For this you should first create a Facebook App, just create it and leave it empty. From this app you will get the Fb App ID and Fb App Secret. Then using this URL:
https://graph.facebook.com/oauth/access_token?type=client_cred&client_id=FB_APP_ID&client_secret=FB_APP_SECRET
you will get the access token.

Related

Upload photos to facebook app page using server-side script

I have a server-side script, that should upload photo-albums to my facebook app page. How can script obtain page access token?
I know it's possible on client-side, when user logs in into facebook, and then asks for page access token using me/accounts query. But server-side script can not authorize as a user. So how this can be done?
UPDATE: I actually found how the same goal - uploading albums to app page from server-side script - can be achieved in another way, by manually creating never expired page token. It's described in here: https://stackoverflow.com/a/43570120/4050723
But still I wonder, if this can be done programmaticaly, from script.
There is no way to generate a User Token server side, it always needs user interaction. And you do need a User Token to get a Page Token.
You can create an Extended Page Token with an Extended User Token though, as you have found out already.
More information:
https://developers.facebook.com/docs/facebook-login/access-tokens/
http://www.devils-heaven.com/facebook-access-tokens/

Facebook auto-reauth

I wrote a python script that runs on a cron job on my server that posts to my facebook account for me using my own facebook app. Unfortunately, every couple months, I get:
Response: {u'error': {u'message': u'Error validating access token: Session has expired...', u'code': 190, u'type': u'OAuthException', u'error_subcode': 463}}
Every time this occurs, I have to log into facebook, get a new code -> access token.
I'd like to fully automate this so that I can get a new access token without ever logging into facebook. What's the best way to do this?
I'd prefer a way to do this without storing my password on my server or accessing facebook via curl (which would be a hack), but--worst comes to worst--I suppose I could just login to facebook with curl to get the code -> access token.
Please don't tell me to use offline_access (as most of my searches show). That's been deprecated.
TIA!
edit: here's how I currently re-auth (this requires me to log into facebook):
log into facebook in browser
visit in browser: https://www.facebook.com/dialog/oauth?client_id=XXX&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=publish_actions
extract $code returned from facebook
curl "https://graph.facebook.com/oauth/access_token?client_id=XXX&redirect_uri=https://www.facebook.com/connect/login_success.html&client_secret=YYY&code=$code"
store updated access_token that's returned from facebook
That is exactly how it is supposed to be. There is no way (anymore) to get a User Access Token that is valid forever. No app should be allowed to use a Token of a User who did not open the App in more than 60 days.
One solution is to create a Facebook Page and use an Extended Page Access Token, because those are valid forever. Here are a couple of links with all the information you need:
https://developers.facebook.com/docs/facebook-login/access-tokens/
http://www.devils-heaven.com/facebook-access-tokens/
http://www.devils-heaven.com/extended-page-access-tokens-curl/

Post to user page from app on server

I have got the APP token using https://graph.facebook.com/oauth/access_token
I have got the usser access token using https://www.facebook.com/dialog/oauth?client_id
I now what to to publish on the user FaceBook page and it appears I need to USER_ID
Where is that?
I am looking to publish using https://graph.facebook.com/[USER ID]/feed
The user id is the number or facebook user of the user where you want to post...
When you see the url from your home you see:
https://www.facebook.com/profile.php?id=4
https://www.facebook.com/zuck
In this case you can use 4 or zuck for that.

how do i get app secret for my page?

I need to grab the latest post from a facebook like page. I'm an administrator on the page so I have the app id but reading the docs looks like I need the app secret so I can then get an access token. So I followed instructions and went to the developer app. Problem is the page doesn't show up as an app so I can't access the ifo I need
When I visit https://graph.facebook.com/MY_PAGE_ID/feed?limit=1 i just get an oauth exception saying I need an access token.
Any ideas?
Just in case anyone gets similarly confused.....Turns out I needed to create an app in order to get an access token and secret which I could then use in my url...which looks like this:
https://graph.facebook.com/my_app_id_here/feed?access_token=My_access_token_here
The feed? bit gets me the pages news feed and the access token was generated once I had created an app using my facebook account. The app id can be obtained by going to the like page and clicking on the edit tab next to about (You must be an admin of that page to see this).

facebook: how to store and retrieve access_token on server side?

I am trying to understand, how facebook authentication works and how the flow should look like. I am working with Google App Engine and I have managed to obtain first the code and then access token. Using it I can for example retrieve user's friends list. This is all cool.
However, how can I store this access_token? I wan to allow my user to access different pages in my facebook app and I will need this access token on those pages. How can I store it and how can I retrieve it? Or maybe no matter which page user accesses I first need to get the code and then access token and only then can I perform some operations on his behalf?
I don't want to use javascript sdk for now. Is it possible to do it all from server side?
how can I store this access_token? I
wan to allow my user to access
different pages in my facebook app and
I will need this access token on those
pages. How can I store it and how can
I retrieve it?
just store the access_token in the datastore.
https://github.com/facebook/python-sdk/blob/master/examples/appengine/example.py
line 50.
It is from the facebook python sdk.
https://github.com/facebook/python-sdk/
If you don't want to use javascript sdk, you need to see this document. It has all details for facebook Oauth.
http://developers.facebook.com/docs/authentication/
While facebook redirect your user to your page assigned by redirect_uri. It will give your the code (A_CODE_GENERATED_BY_SERVER), then your server can get the user's access_token/facebook id with server side facebook api + code. Then you can login your user (set the session/cookie) and do whatever you want.