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

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

我使用的代码如下:

picture_url = @user.get_picture("1000000111") 

其中@user是使用Facebook身份validation制作的Graph API对象…

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

我不知道考拉,但如果你要求用户的标识符(以graph.facebook.com/odedharth/picture的forms),你可以得到它。

在轨道上获取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 

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

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

 @rest = Koala::Facebook::API.new(oauth_access_token) @rest.fql_query(my_fql_query) @rest.fql_multiquery(fql_query_hash) # convenience method 

要编写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())") 

您还可以使用get_connection()获取所有朋友的照片,如下所示

 friends = @graph.get_connections("me", "friends?fields=id,name,picture.type(large)") 

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

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

使用您从oauth收到的访问令牌

 graph = Koala::Facebook::GraphAPI.new(access_token) profile_path = graph.get_picture(uid) 

要获取路径,然后显示它

 <%= image_tag profile_path %> 

这对我有用,应该适合你..

  USER_PROFILE_OPTIONS = { fields: %w(id first_name last_name hometown email gender birthday picture.width(320)) }.freeze @graph = Koala::Facebook::API.new(oauth_access_token) @graph.get_object('me', USER_PROFILE_OPTIONS) 

请参阅: https : //developers.facebook.com/docs/graph-api/reference/user/picture/