[英]How do I manage pages of a user that facebook authed on my site?
I have an app that uses omniauth to facebook auth. 我有一个使用omniauth进行Facebook身份验证的应用程序。 I also have a user access token.
我也有一个用户访问令牌。 I'm using koala to access facebook graph api.
我正在使用考拉访问Facebook图形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.
但是,我不确定如何向我的用户访问令牌传递考拉以实际访问我的用户管理的Facebook页面。
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 这将返回您的用户有权访问的页面的集合,在每个页面的内部都有一个“ access_token”属性,该令牌是您用来管理该特定页面的令牌
Since you are using omniauth for Facebook Authentication, you can add scope: manage_pages
while defining provider in omniauth.rb
由于您正在使用omniauth进行Facebook身份验证,因此可以在
omniauth.rb
定义提供程序时添加scope: manage_pages
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 现在,当您有能力时,使用Graph API调用使用Koala获取用户拥有的页面列表
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. 使用上一次调用的结果中的
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)
Use the page token to authenticate as a page and use Graph API as normal 使用页面令牌作为页面进行身份验证,并正常使用Graph 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.