如何使用Koala gem获取Facebook用户的图片?

Apo*_*ena 6 ruby-on-rails facebook-graph-api

我正在尝试使用他们的Facebook ID获取Facebook用户的朋友的图片,但使用以下代码返回nil而不是图片网址...

我使用的代码如下:

picture_url = @user.get_picture("1000000111")
Run Code Online (Sandbox Code Playgroud)

其中@user是使用Facebook身份验证制作的Graph API对象...

这段代码中缺少什么?提前致谢.

Pun*_*eth 15

在轨道上获取ruby中的所有facebook朋友图像.你可以使用Koala gem https://github.com/arsduo/koala

它有很好的涂抹

要获得facebbok朋友,你可以使用

@graph = Koala::Facebook::API.new(oauth_access_token)
friends = @graph.get_connections("me", "friends")
friends.each do |f|
  f[:image]= @graph.get_picture(f["id"])
end
Run Code Online (Sandbox Code Playgroud)

但是这种方法需要花费太多时间,因为你必须为每个朋友发送请求.

为避免这种情况,您可以使用考拉支持的REST API,

@rest = Koala::Facebook::API.new(oauth_access_token)
@rest.fql_query(my_fql_query) 
@rest.fql_multiquery(fql_query_hash) # convenience method
Run Code Online (Sandbox Code Playgroud)

要编写Fql查询,您可以使用facebook表结构,您可以在http://developers.facebook.com/docs/reference/fql/找到它.

要获得Facebook uid,名称和图片你可以使用这个..

@rest.fql_query("select uid, name, pic_square from user where uid in (select uid2 from friend where uid1 = me())")
Run Code Online (Sandbox Code Playgroud)


Mou*_*mir 13

您还可以使用get_connection()以下方式获取所有朋友的照片

friends = @graph.get_connections("me", "friends?fields=id,name,picture.type(large)")
Run Code Online (Sandbox Code Playgroud)


bal*_*anv 5

我通过图像标签直接使用Cody指定的url来获取图像,如下所示



img  src="http://graph.facebook.com/10000010000001/picture" />

img  src="http://graph.facebook.com/user_profile_id/picture" />



Ode*_*rth 1

我不了解 Koala,但如果您询问用户的标识符(以 的形式graph.facebook.com/odedharth/picture),您就可以获得它。