Tuesday, May 20, 2014

Post on Facebook Page using Facebook Graph API

Actually, Facebook offers a great API that is flexible and simple. However, I feel that is hard to get started if lacking the passion to read their document.

* I'm using hello.com as my host server for example here.

Preparation

  • Read Facebook Official Document - Access Token
  • Create a Facebook App on Developer Facebook
    • find out App ID and App Secret, you will need them later
    • put http://hello.com in Settings->Website->Site URL and Mobile Site URL
    • since we will need "manage_pages" permission, select it from the list and submit for approve by Facebook (it takes around a week)
  • Create a simple file that prints GET parameters on your server code. Ex, put <?php print_r($_GET);?> as index.php in hello.com

Variables

  • App ID
  • App Secret
  • URL (hello.com)
  • code
  • access_token
  • Facebook Page ID
  • message (the content you want to post on the page)

Facebook Graph APIs

  • https://www.facebook.com/dialog/oauth?client_id=<App ID>&redirect_uri=http://hello.com/&scope=manage_pages,publish_stream
    • you will get "code" from GET parameters in hello.com/index.php
  • https://graph.facebook.com/oauth/access_token?client_id=<App ID>&redirect_uri=http://hello.com/&client_secret=<App Secret>&code=<code>
    • you will get "access_token" and its expire time
  • https://graph.facebook.com/me/accounts?access_token=<access_token>
    • this checks the user's account information (his/her Facebook pages)
  • https://graph.facebook.com/<Facebook Page ID>/feed?access_token=<access_token>&message=<message>
    • the message is posted on the page

Reference