【发布时间】:2015-03-09 04:58:17
【问题描述】:
我正在使用带有omniauth-facebook 的rails 4.2 来使用facebook 注册并计划使用koala 来检索current_user 的facebook 数据(专辑、朋友列表等)。在用户注册/登录时,我正在创建/更新用户详细信息,如下所示
def self.from_omniauth(auth)
# Get 60 days auth token
oauth = Koala::Facebook::OAuth.new(Rails.application.secrets.facebook['app_id'], Rails.application.secrets.facebook['app_secret'])
new_access_info = oauth.exchange_access_token_info auth.credentials.token
new_oauth_token = new_access_info['access_token']
new_oauth_expires_at = DateTime.now + new_access_info['expires'].to_i.seconds
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.password = Devise.friendly_token[0,20]
#user.image = auth.info.image
user.email = auth.info.email
user.oauth_token = new_oauth_token
user.oauth_expires_at = new_oauth_expires_at
user.save!
end
end
根据 koala 的 Graph API 部分,我们需要指定 访问令牌,我们可以通过 Facebook 的 Graph API Explorer 获得。我为我的应用程序创建了一个访问令牌,具有检索用户详细信息、喜欢、朋友和照片的权限。
一些控制台输出:
pry(main)> a.class
=> User
pry(main)> a.oauth_token
=> "CAAHTERFDSFSREWrWErwelrinwoerxwerxqw"
pry(main)> graph = Koala::Facebook::API.new a.oauth_token
=> #<Koala::Facebook::API:0x00000008198810 @access_token= "CAAHTERFDSFSREWrWErwelrinwoerxwerxqw", @app_secret=nil>
pry(main)> friends = graph.get_connections("me", "friends")
=> {...}
pry(main)> photos = graph.get_connections("me", "photos")
=> {...}
pry(main)> photos.count
=> 25
pry(main)> p = graph.get_object "me"
=> {"id"=>"10206089",
"email"=>"prasad@yahoo.co.in",
"first_name"=>"Prasad",
"gender"=>"male",
"last_name"=>"Surase",
"link"=>"https://www.facebook.com/app_scoped_user_id/10206089/",
"locale"=>"en_US",
"name"=>"Prasad Surase",
"timezone"=>5.5,
"updated_time"=>"2014-12-14T06:12:07+0000",
"verified"=>true}
我的问题是,我们能否检索 current_user 在特定日期范围内创建的照片。这可能吗?我需要每张图片的图片标题、图片喜欢和图片 cmets。
【问题讨论】:
-
你需要增加这里提到的分页限制..developers.facebook.com/docs/graph-api/using-graph-api#paging
标签: ruby-on-rails koala