2014-01-17 60 views
2

我有一個應用程序,使用omniauth Facebook的身份驗證。我也有一個用戶訪問令牌。我正在使用考拉來訪問Facebook圖形API。但是,我不確定如何通過考拉我的用戶訪問令牌實際訪問我的用戶管理的Facebook頁面。如何管理我的網站上的Facebook用戶的頁面?

如何獲取我的用戶管理的頁面並傳遞訪問令牌來執行此操作?

回答

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

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

這個返回的頁面集合,它的用戶具有訪問權限,每個頁面裏面有一個「ACCESS_TOKEN」屬性,該標記是你需要使用來管理特定頁面

0

的一個由於您使用omniauth對於Facebook的身份驗證,就可以同時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 

這個定義提供商將詢問管理頁面所需的權限的用戶添加scope: manage_pages

現在,當你有permision,使用考拉獲得頁面的用戶列表已經使用圖形API調用

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

使用以前調用的結果access_token財產。

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

使用頁面標記爲一頁面以驗證和使用圖形API正常

@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) 
相關問題