I'm trying to use the Graph API to pull a list of Events from my private group page to put on my website about the group so that people can see all of our events on the site. I can get the data I want on the Graph API Explorer tool but I'm not able to replicate it in PHP.
I'm not using the SDK or anything, just a simple GET call to the API to get the data I want. It worked using the access token from the Explorer tool for about an hour and then the token didn't work anymore. I've been trying to figure out how to authenticate properly but all of the docs on the topic only show how to authenticate the user currently on the page using the Javascript SDK in tandem with the server-side API calls.
What I want is to just load the data on page load using my account on the server side to pull the events and put them on the page for anyone to see, not requiring anyone to have a Facebook account and give my app access to their info. How can I do this?
Summary: By storing a long-lived access token on the server, you may use that to call the Graph API on the server-side, in behalf of the user (which is you), even without the user's presence (because you won't be there when your friends/audience views your website).
Long explanation
Your biggest concern is that tokens expire, as you just experienced when using the API Explorer Token. This is expected behavior, we call these short-lived tokens which last for around a couple of hours. If you want it to last longer, you'll need a long-lived token. Read more on the topic here.
In your situation, you may get this long-lived token manually, and use it on the PHP SDK to fetch the data you need from the backend. No one on the client-side needs to connect. Just make sure you come back after 60 days (default expiration of long-lived tokens) and refresh a new token and update your script with it.
Note that generating a long-lived token requires your to pass
client_id
andclient_secret
, as explained on the developer docs. If previously you are just using Graph API Explorer, this time you may need to create your own application to get these credentials.