简体   繁体   中英

How do I manage pages of a user that facebook authed on my site?

I have an app that uses omniauth to facebook auth. I also have a user access token. I'm using koala to access facebook graph api. However, I'm not sure how to pass koala my user access token to actually get access to the facebook pages that my user manages.

How do I get the pages that my user manages and pass the access token to do that?

api = Koala::Facebook::API.new(access_token)

pages = api.get_connections(user_id, "accounts")

this returns the collection of pages for which your user has access, inside of each page there's an "access_token" property, that token is the one that you need to use to manage that specific page

Since you are using omniauth for Facebook Authentication, you can add scope: manage_pages while defining provider in omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], scope: 'publish_actions, email, manage_pages']
end

This will ask the user about the permissions required for managing pages.

Now, when you have the permision, Use Koala to get a list of pages user has using a Graph API call

koala = Koala::Facebook::API.new(@access_token)

# 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')

Use the access_token property from the result of the previous call.

# 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

@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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM