How to get the picture of Facebook User using Koala gem?

前端 未结 6 1250
伪装坚强ぢ
伪装坚强ぢ 2021-02-09 17:14

I am trying to fetch the picture of a Facebook user\'s friends using their facebook ids, but using the following code returns nil instead of the picture url...

The code

相关标签:
6条回答
  • 2021-02-09 18:08

    I got the image working by directly using url specified by Cody in image tag as below

    
    
    img  src="http://graph.facebook.com/10000010000001/picture" />
    
    img  src="http://graph.facebook.com/user_profile_id/picture" />
    
    
    
    0 讨论(0)
  • 2021-02-09 18:09

    To get all facebook friend images in ruby on rails. you can use Koala gem https://github.com/arsduo/koala

    It has got nice doumentation

    To get the facebbok friend you can use

    @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
    

    But this method will take too much time because you will have to send request for each friend.

    To avoid this you can use The REST API which koala supports,

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

    To Write Fql query you can use the facebook table structure which you can find at http://developers.facebook.com/docs/reference/fql/

    to get Facebook uid , name and pic you can use this one..

    @rest.fql_query("select uid, name, pic_square from user where uid in (select uid2 from friend where uid1 = me())")
    
    0 讨论(0)
  • 2021-02-09 18:11

    I don't know about Koala, but if you ask for the user's identifier ( in the form of graph.facebook.com/odedharth/picture ) you can get it.

    0 讨论(0)
  • 2021-02-09 18:11
      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)
    

    See: https://developers.facebook.com/docs/graph-api/reference/user/picture/

    0 讨论(0)
  • 2021-02-09 18:17

    Use the access token you've received from oauth

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

    To get the path, then to display it

    <%= image_tag profile_path %>
    

    This works for me, and should for you..

    0 讨论(0)
  • 2021-02-09 18:20

    You can also get all friends with their pictures using get_connection() as follows

    friends = @graph.get_connections("me", "friends?fields=id,name,picture.type(large)")
    
    0 讨论(0)
提交回复
热议问题