Search Unity

Question about fb api.

Discussion in 'Scripting' started by Gorlog, Apr 13, 2017.

  1. Gorlog

    Gorlog

    Joined:
    Jan 20, 2011
    Posts:
    201
    Hello i figure it out to how to get my profile picture on facebook im not familiar on facebook api.

    So my problem is i challange a friend using on same app on android i click and challange him but on callback sucsess i need him or her profile picture link like this or string.

    FB.API("/me/picture?type=square&height=128&width=128", HttpMethod.GET, DisplayProfilePic);

    i cant find any document on fb page

    Thanks for advice and sorry for my bad english ^_^
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    If they are friends that have approved your app, you will get them by requesting friends in your query. If they have not approved your app, but your app is a game, you can use invitable friends to get their picture still. Friends will also provide their FB id which can be used to retrieve the picture.
     
    Gorlog likes this.
  3. Gorlog

    Gorlog

    Joined:
    Jan 20, 2011
    Posts:
    201
    Wow thx for fast reply one more question how can i get picture can u write me example code ?

    Code (CSharp):
    1.  public void ShareWithUsers()
    2.     {
    3.         FB.AppRequest(
    4.             "Come and join me, i bet u cant beat my score",
    5.             null,
    6.             new List<object>() {"app_users"},
    7.             null,
    8.             null,
    9.             null,
    10.             null,
    11.             ShareWithUsersCallback
    12.         );
    13.  
    14.     }
    15.     void ShareWithUsersCallback(IAppRequestResult result)
    16.     {
    17.         if (result.Cancelled)
    18.         {
    19.             Debug.Log("challange Cancel");
    20.             GameObject.Find("CallBacks").GetComponent<Text>().text = "challange Cancel";
    21.         }
    22.         else if (!String.IsNullOrEmpty(result.Error))
    23.         {
    24.             Debug.Log("challange on error");
    25.             GameObject.Find("CallBacks").GetComponent<Text>().text = "challange on error";
    26.         }
    27.         else if (!String.IsNullOrEmpty(result.RawResult))
    28.         {
    29.             Debug.Log("Sucsess on challange");
    30.             GameObject.Find("CallBacks").GetComponent<Text>().text = "Sucsess on challange";
    31.             /// i need to on sucsess get picture link
    32.         }
    33.     }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    string queryString = "/me?fields=id,email,first_name,last_name,picture,friends.limit(1000).fields(first_name,last_name,id,picture),invitable_friends.limit(1000).fields(first_name,last_name,id,picture)";

    FB.API(queryString, HttpMethod.GET, APICallback);
     
    Gorlog likes this.
  5. Gorlog

    Gorlog

    Joined:
    Jan 20, 2011
    Posts:
    201



    i try like this after challage a friend but no luck



    Code (CSharp):
    1.     public Sprite FriendProfilePic { get; set; }
    2.     public void GetProfilePicFriends()
    3.     {
    4.         string queryString = "/me?fields=id,email,first_name,last_name,picture,friends.limit(1000).fields(first_name,last_name,id,picture),invitable_friends.limit(1000).fields(first_name,last_name,id,picture)";
    5.  
    6.         FB.API(queryString, HttpMethod.GET, DisplayProfilePicFriends);
    7.         FB.GetAppLink(DealWithAppLink);
    8.     }
    9.  
    10.     void DisplayProfilePicFriends(IGraphResult result)
    11.     {
    12.         if (result.Texture != null)
    13.         {
    14.             FriendProfilePic = Sprite.Create(result.Texture, new Rect(0, 0, 128, 128), new Vector2());
    15.         }
    16.     }
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    You need to use that to get your friends info. Usually you can do this at the start. For example, we log in and then we do the query. It's a json file, so it's easy to convert it into a list of friends. This then will include the friends picture url. When you challenge a friend you will need to find their picture url and then use that url to get their picture.

    What you are trying isn't going to work as this is a json file with players info, friends info, and invitable friends info.

    To add, we use www request to get the pictures just when we need them. Usually only when doing highscore list. You can store these images also, just don't grab them all upfront, just when you might need them.
     
    Last edited: Apr 13, 2017
  7. Gorlog

    Gorlog

    Joined:
    Jan 20, 2011
    Posts:
    201
    Hımm this is okey then ? im not test it

    Code (CSharp):
    1.     public void GetFirendPic()
    2.     {
    3.         FB.API("/me/invitable_friends?fields=picture.width(128).height(128).type(normal)", HttpMethod.GET, DisplayFirendPic);
    4.         FB.GetAppLink(DealWithAppLink);
    5.     }
    6.  
    7.     void DisplayFirendPic(IGraphResult result)
    8.     {
    9.         if (result.Texture != null)
    10.         {
    11.             FriendProfilePic = Sprite.Create(result.Texture, new Rect(0, 0, 128, 128), new Vector2());
    12.             GameObject.Find("friend_image").GetComponent<Image>().sprite = FriendProfilePic;
    13.  
    14.         }
    15.     }
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Result doesn't return a texture from that api call. There is probably a different api query that returns a texture.
    json takes more explaining than I can do right now, but result.RawResult is your json string that is returned.

    You'll need to create a class that you can deserialize to. Or, depending on what json asset you use (unity added JsonUtility, but there are several others including json.net) You'll convert the string into a class. In my case, I store all this data into a class of facebook data where I can access all the friend data. That way when I create list of friends, I can access their profile image look to display their pic in stuff like leaderboards or invite list.
     
    Gorlog likes this.
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Were you able to figure out json and the Facebook api?