-
Notifications
You must be signed in to change notification settings - Fork 464
Acting as a Page
Valendea Amalia edited this page Jul 23, 2018
·
12 revisions
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_pages
, 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 for 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('me', 'feed', :message => message, :link => link_url) #post as link to page
@page_graph.put_picture(picture_url, {:message => "hello"}, page_id) #post as picture with caption
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.