The reason to write this post, it’s because I found tricky to use the Facebook API to publish on my own pages. Show
Be aware that I have no previous experience on the Facebook API, and I got confused on how to get the required details to actually play with the API, this post should serve as a note for myself, and hopefully, it will help others struggling with this. BackgroundOne of our projects (cazadescuentos.net) has a Facebook fan page, we started publishing the interesting discounts found by our app, but it’s a tedious, and time consuming task. I got the idea to build a bot to keep the fan page updated automatically. By now, you should see the automated posts our bot has created, I won’t cover how to do that, as it’s a simple API call once you get the right access token. ConstraintsThe official docs aren’t clear if posting to your own page requires app review, and given my tests, you need the review. Unfortunately, such reviews are paused for individuals indefinitely, but you can still apply if you are behind a business. The instructionsNow the actual step by step to get it working. The first task to do, is to update your account to be a developer account, which can be done in developers.facebook.com. Create an appYou can’t get a token unless you create an app, which is done on developers.facebook.com/apps, in this case the
Now just click on The summary after this is:
Get a short-lived user tokenWhile you might try to get the page token directly, it won’t work, Facebook will start complaining about your app not been verified, etc. if, you know, or you find a simpler way to do so, I’d thank you for such information. Go to Tools -> Graph API Explorer On the right, choose these options, and then, click on the
A popup should open, asking you for permission to login, which you should approve: The Add permissions to your short-lived user access tokenAssuming you are on the previous screen, click on the You will be required to authorize the login again, but now, you are asked about which pages you will authorize to be managed by this token, choose your own, and click on This time, you get a warning about Facebook not approving the permissions, the text being:
Just ignore the warning, make sure the permission for creating content on the page is enabled, and, click on Now you get a confirmation that your app got linked to your page, there is a hyperlink where you could go to remove such integration if you ever need to, click on the Now you have a short-lived user access token allowing you to post on your page. Get the long-lived user access tokenOnce you have the long-lived user access token (expires in 1 hour) allowed to post on your page, you can get a long-lived user access token. Let’s replace the following placeholders and invoke this API:
To get the client secret, go to your app dashboard, which you can choose on the My Apps menu, then, choose Then, click on the Now, the actual call:
The API call returns something like:
Get the actual page access tokenWe are almost done, now you just need to invoke another API, be sure to replace the How to get the page id
is out of the scope on this post, I my case, I just visited my fan page, clicked on
Which returns something like:
That’s your long-lived page access token! It’s a token that never expires. Try itNow, let’s create a post by invoking the Facebook API, replace the place holders making sure to provide the new token, and enjoy:
You should expect a response like:
TroubleshootingFacebook has a very useful tool for debugging access token, find it on the Tools -> Access Token Debugger menu, there you can input the generated tokens to make sure what’s their scope, lifetime, permissions, etc. The page token we got should says:
MoreIt’s worth that you check the official docs for the pages API. Also, note that no one besides you will be able to see what you post until you switch to the live mode, by default your app is on development mode, see the official docs for more details, once you switch to live mode, the token won’t work unless your app has been verified. How do I get my Facebook page ID?To find your Page ID:. Tap in the top right of Facebook.. Tap Pages.. Go to your Page.. Tap About.. Scroll down to see your Page ID.. How do I get data from Facebook Graph API?Open the Graph Explorer in a new browser window. This allows you to execute the examples as you read this tutorial. The explorer loads with a default query with the GET method, the lastest version of the Graph API, the /me node and the id and name fields in the Query String Field, and your Facebook App.
How do I get Facebook Page Insights on graph API?You can access Page Insights by typing /insights after the name of your page in the URL field. This command will retrieve all of the Insights associated with your Page. Type "/insights" after your company name. Click the Submit button.
What can I do with Facebook Graph API?The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.
|