Acting as a Page
Clone this wiki locally
Sometimes, a Facebook user isn't just a Facebook user, she's a Page. The Graph API makes it straightforward to act as a Page a user manages.
First, you need a user access token with the
manage_pages permission (you may need other permissions, such as
publish_stream, depending on what you want to do) and authenticate as this user:
@user_graph = Koala::Facebook::API.new(user_access_token)
Then, you can either retrieve a list of all the pages the user manages or a single page access token for a given page id:
# retrieve collection fo all your managed pages: returns collection of hashes with page id, name, category, access token and permissions pages = @user_graph.get_connections('me', 'accounts') # get access token for first page first_page_token = pages.first['access_token'] # or: retrieve access_token for a given page_id page_token = @user_graph.get_page_access_token(page_id)
Use the page token to authenticate as a page and use Graph API as normal (see Page Graph API Reference for the extensive documentation):
@page_graph = Koala::Facebook::API.new(page_token) @page_graph.get_object('me') # I'm a page @page_graph.get_connection('me', 'feed') # the page's wall @page_graph.put_wall_post('post on page wall') # post as page, requires new publish_pages permission @page_graph.put_connections(page_id, 'feed', :message => message, :picture => picture_url, :link => link_url)
To a large extent, acting like a page on Facebook is similar to acting like a user -- the same methods and calls will usually work.