Search code examples
facebook-graph-apicronserver-sideaccess-token

How do I programmatically get a FB token so that I can run a Graph API request via a script?


Facts:

I'm running a FB contest where users are entered into a drawing simply by liking a specific post.

I can call $postid/likes in the Graph API Explorer while using the token generated by the Graph API Explorer and receive the proper information. It's a simple result with user id/name pair which is all I need to generate a random winner.

I'm running a daily cron that collects all new likes for the post and puts them in a database so that I can track the number of entrants and pull out a winner from the pool easily. The script works and I get the expected data if I copy out the token from the Graph API Explorer call above and plug it into the script.

Problem:

Since the script above works when I copy out the active temporary token from the Graph API Explorer, shouldn't I be able to do this programmatically? I can't figure out how I am supposed authenticate when my daily cron runs (no user interaction) in order to get a new, valid, non-expired token. Since this is a cron running from the server, I obviously can't authenticate through a user dialog.


Solution

  • You can create an app and then authenticate as that app. Access tokens granted to an app don't expire so you'll be able to code that into your cron job.

    Tread carefully with this. It looks like your contest violates the Facebook promotions guidelines:

    You must not condition registration or entry upon the user liking a Wall post.