Nepal on Rails

millisami's rants!

How to Get Facebook Page_id and Access_token to Publish on Its Wall

Its always tricky to post on the wall of the FB Page. It requires PAGE_ID and ACCESS_TOKEN (Not the user’s access token) and so many questions on how to get facebook page id and access token.

The FB Developers webapp way

First create a page by visiting http://www.facebook.com/pages/create.php?ref_ type=sitefooter But this is optional. If you’ve already created the page, the just skip this part. Create a facebook page

  1. Create an App

Create App Dialog

  1. Created App admin page

App Admin Page

  1. Graph API Explorer

Choose the App

  1. Get Access Token

In the Extended Permissions tab, select manage_pages and publish_stream and click Get Access Token Extended
Permissions

  1. Authorize you account

Authorize

  1. Allow Permission

Grant Permission

  1. Copy the blue-highlighted temporary access token

Temporary Access Token

  1. Permanent Access Token

The above temporary access token is also valid. But its limited to 60 days. Its better to have the permanent access token so that it works in the long run, though I forget to updated periodically.

To get this, go the following url replacing the TEMPORARY_ACCCESS_TOKEN value with the temporary access token.

https://graph.facebook.com/me/accounts?access_token=TEMPORARY_ACCCESS_TOKEN

Permanent Access Tokens

Ruby Way

  1. First we need to get the PAGE_ID of the page. To get this, visit https://www.facebook.com/pages/

And on the left sidebar under Pages section, select the page. Page

  1. Copy the Page id from the address bar

Page ID

  1. Here I’m using the fb_graph gem

require ‘rubygems’ require ‘fb_graph’ require ‘pry’ FbGraph.debug!

PAGE_ID = “349835561791721” YOUR_ACCESS_TOKEN = “……”

owner = FbGraph::User.me(USER_ACCESS_TOKEN) pages = owner.accounts

page = pages.detect do |page| page.identifier == PAGE_ID end

binding.pry page

There is the page access_token

Enjoy!!