Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Facebook Leaderboard not showing friend scores

Discussion in 'Scripting' started by Reymus, Jun 2, 2015.

  1. Reymus

    Reymus

    Joined:
    Apr 27, 2015
    Posts:
    44
    Hi all.

    I followed the tutorial on setting up the Facebook Scores API that Greyzoned spelled out on YouTube, and had no trouble setting it up. When I use test users, the test users can see each other's scores. I then submitted a request to Facebook to allow publish_actions, which was then approved. Now, all of my users are able to see their own scores posted on a leaderboard, but it doesn't show the scores of anybody else on their friend list, as I understand it should.

    I'm sure I'm missing something, but I can't for the life of me figure out what.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class FBMain : MonoBehaviour {
    7.  
    8.     public GameObject FBLoggedIn;
    9.     public GameObject FBNotLoggedIn;
    10.     public GameObject ScoreEntryPanel;
    11.     public GameObject ScoreScrollList;
    12.     public GameObject highScoresPanel;
    13.  
    14.     public Text HighScoreText;
    15.  
    16.     private Dictionary<string, string> profile =null;
    17.  
    18.     public GameController gameController;
    19.  
    20.     private List<object> scoresList = null;
    21.  
    22.     private List<object> myScore = null;
    23.  
    24.     public int myHighScore;
    25.  
    26.     public void Start()
    27.     {
    28.         if (FB.IsLoggedIn) {
    29.  
    30.  
    31.  
    32.             FB.API ("/me/scores", Facebook.HttpMethod.GET, delegate(FBResult result) {
    33.                 if (result == null) {
    34.                     HighScoreText.text = "High Score: 0";
    35.                 } else {
    36.  
    37.  
    38.                     myScore = Util.DeserializeScores (result.Text);
    39.                     Debug.Log (result.Text);
    40.  
    41.                     foreach (object score in myScore) {
    42.                         var entry = (Dictionary<string, object>)score;
    43.                
    44.                         Debug.Log(score);
    45.                         HighScoreText.text = "High Score: " + entry ["score"].ToString ();
    46.                
    47.                         myHighScore = int.Parse (entry ["score"].ToString ());
    48.  
    49.                     }
    50.                 }
    51.             });
    52.         } else
    53.         {
    54.             HighScoreText.text = "High Score Unknown";
    55.         }
    56.     }
    57.  
    58.     void Awake()
    59.     {
    60.         FB.Init (SetInit, OnHideUnity);
    61.  
    62.     }
    63.  
    64.     private void SetInit()
    65.     {
    66.         Debug.Log ("FB Init Done.");
    67.  
    68.         if (FB.IsLoggedIn) {
    69.             Debug.Log("FB Logged In");
    70.             DealWithFBMenus(true);
    71.         } else {
    72.             DealWithFBMenus(false);
    73.         }
    74.     }
    75.  
    76.     private void OnHideUnity(bool isGameShown)
    77.     {
    78.         if (!isGameShown) {
    79.             Time.timeScale = 0;
    80.         } else {
    81.             Time.timeScale = 1;
    82.         }
    83.     }
    84.  
    85.     public void FBLogin()
    86.     {
    87.         FB.Login ("email,publish_actions",AuthCallback);
    88.  
    89.  
    90.            
    91.     }
    92.  
    93.     void AuthCallback (FBResult result)
    94.     {
    95.         if (FB.IsLoggedIn) {
    96.             Debug.Log ("FB Login worked");
    97.             DealWithFBMenus(true);
    98.         } else {
    99.             Debug.Log ("FB Login Failed");
    100.             DealWithFBMenus(false);
    101.         }
    102.     }
    103.  
    104.     void DealWithFBMenus(bool isLoggedIn)
    105.     {
    106.         if (isLoggedIn) {
    107.             FBLoggedIn.SetActive(true);
    108.             FBNotLoggedIn.SetActive (false);
    109.  
    110.  
    111.  
    112.  
    113.  
    114.         } else {
    115.             FBLoggedIn.SetActive(false);
    116.             FBNotLoggedIn.SetActive (true);
    117.         }
    118.     }
    119.  
    120.  
    121.  
    122.     public void FBShare()
    123.     {
    124.         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    125.         if (gameControllerObject != null) {
    126.             gameController = gameControllerObject.GetComponent <GameController> ();
    127.         }
    128.         if (gameController == null) {
    129.             Debug.Log ("Cannot find 'GameController' script");
    130.         }
    131.  
    132.         FB.Feed (
    133.             linkCaption: "I just got a score of " + gameControllerObject.GetComponent<GameController>().score + " in Crystal Cracker! Can you beat me?",
    134.             linkName: "Play Crystal Cracker Now",
    135.             link: "http://roamsoftstudios.com/crystalcracker",
    136.             picture: "http://roamsoftstudios.com/wp-content/uploads/2015/05/crystalcrackerscreen.png"
    137.             );
    138.     }
    139.  
    140.     public void InviteFriends()
    141.     {
    142.         FB.AppRequest (
    143.             message: "Give Crystal Cracker a try on iPhone or Android!  It's too fun!",
    144.             title: "Invite your friends to play Crystal Cracker"
    145.         );
    146.     }
    147.  
    148.     //All Scores API related items
    149.  
    150.     public void QueryScores()
    151.     {
    152.         highScoresPanel.SetActive (true);
    153.         FB.API ("app/scores?fields=score,user.limit(30)", Facebook.HttpMethod.GET, ScoresCallback);
    154.     }
    155.  
    156.     public void ScoresCallback(FBResult result)
    157.     {
    158.  
    159.  
    160.         Debug.Log ("Scores Callback: " + result.Text);
    161.  
    162.  
    163.         scoresList = Util.DeserializeScores (result.Text);
    164.  
    165.         foreach (Transform child in ScoreScrollList.transform)
    166.         {
    167.             GameObject.Destroy (child.gameObject);
    168.         }
    169.  
    170.         foreach (object score in scoresList) {
    171.             var entry = (Dictionary<string, object>) score;
    172.             var user = (Dictionary<string, object>) entry["user"];
    173.  
    174.  
    175.  
    176.             GameObject ScorePanel;
    177.             ScorePanel = Instantiate (ScoreEntryPanel) as GameObject;
    178.  
    179.             ScorePanel.transform.parent = ScoreScrollList.transform;
    180.             ScorePanel.transform.localScale = new Vector2(1,1);
    181.  
    182.             Transform ThisScoreName = ScorePanel.transform.Find ("FriendName");
    183.             Transform ThisScoreScore = ScorePanel.transform.Find ("FriendScore");
    184.             Text ScoreName = ThisScoreName.GetComponent<Text>();
    185.             Text ScoreScore = ThisScoreScore.GetComponent<Text>();
    186.  
    187.             ScoreName.text = user["name"].ToString();
    188.             ScoreScore.text = entry["score"].ToString();
    189.  
    190.             Transform TheUserAvatar = ScorePanel.transform.Find("FriendAvatar");
    191.             Image UserAvatar = TheUserAvatar.GetComponent<Image>();
    192.  
    193.             FB.API (Util.GetPictureURL(user["id"].ToString(), 128, 128), Facebook.HttpMethod.GET, delegate(FBResult pictureResult) {
    194.                 if (pictureResult.Error != null)
    195.                 {
    196.                     Debug.Log (pictureResult.Error);
    197.                 }
    198.                 else
    199.                 {
    200.                     UserAvatar.sprite = Sprite.Create (pictureResult.Texture, new Rect(0,0,128,128), new Vector2(0,0));
    201.                 }
    202.             });
    203.  
    204.         }
    205.     }
    206.  
    207.     public void SetScore()
    208.     {
    209.         if (FB.IsLoggedIn) {
    210.             GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    211.             if (gameControllerObject != null) {
    212.                 gameController = gameControllerObject.GetComponent <GameController> ();
    213.             }
    214.             if (gameController == null) {
    215.                 Debug.Log ("Cannot find 'GameController' script");
    216.             }
    217.  
    218.             var scoreData = new Dictionary<string,string> ();
    219.             scoreData ["score"] = gameController.score.ToString ();
    220.             FB.API ("/me/scores", Facebook.HttpMethod.POST, delegate(FBResult result) {
    221.                 Debug.Log ("Score Submit Result: " + result.Text);
    222.             }, scoreData);
    223.         }
    224.    
    225.     }
    226. }
    227.  
     
    Psyco likes this.
  2. Reymus

    Reymus

    Joined:
    Apr 27, 2015
    Posts:
    44
    bumping for visibility. I still haven't figured this one out, though I've spent the week looking online for more information.
     
    Psyco likes this.
  3. bikram89

    bikram89

    Joined:
    Jun 8, 2015
    Posts:
    1
    What is the log message you are getting at this point?
    Debug.Log ("Scores Callback: " + result.Text);

    Also I would think you will have to add the user_friends permission to your Fb.Login to get friend list
     
  4. Psyco

    Psyco

    Joined:
    Oct 5, 2012
    Posts:
    5
    had exactly the same issue, was fixed by all the players deleting the app and redoing their permissions. Now suddenly we call see eachothers scores.
    Also we made the app live then back to development mode

    Invite friends and share score dont do anything wqhen clicked, no dialog window appears, yet they work fine in unity
     
    Last edited: Aug 26, 2015
  5. Greg-Bassett

    Greg-Bassett

    Joined:
    Jul 28, 2009
    Posts:
    628
    I have same problem, but deleting app and redoing permissions has not worked? when I login using the token for one of my 10 test users I can see all the other test users scores, but if I login using my actual fb account and then login with a friends fb account, we can only see our own scores, and not our own and each others, really frustrating! Anyone any ideas? My app is approved by fb and has the permissions required for the scores api...
     
  6. Psyco

    Psyco

    Joined:
    Oct 5, 2012
    Posts:
    5
    today i am going to remove the facebook scoring entirely and go with a 3rd party scoring system.
    They do not allow you to have a leader board of all people playing the game, only your frineds!
     
  7. Greg-Bassett

    Greg-Bassett

    Joined:
    Jul 28, 2009
    Posts:
    628
    I know, thats why I implemented my own global leaderboard using the dreamlo asset. I am considering releasing my code as an asset which uses the Facebook SDK asset for the friends leaderboard, and the dreamlo asset for the global leaderboard.



     
  8. B_HITMAN

    B_HITMAN

    Joined:
    Oct 11, 2015
    Posts:
    1
    I have the same problem i submitted mt application for publish_actions permissions and it got approved, but i don't see my friends scores. When i use my test users, even before the app got approved, i can see their friends and scores and everything. Did you find a solution to the problem? Can anyone else help please?

    Thank You
     
  9. e140games

    e140games

    Joined:
    Aug 2, 2012
    Posts:
    4
    Just add to FB.Login "user_friends". Example: FB.Login("email,publish_actions,user_friends", AuthCallback);
     
  10. Manu_Kumar

    Manu_Kumar

    Joined:
    Sep 1, 2014
    Posts:
    2
    I also followed the same tutorial ! :p I too have the same problem :/ Any suggestions?
     
  11. javier-mazzurco

    javier-mazzurco

    Joined:
    Jan 22, 2016
    Posts:
    6
    Hi. Same problem for me. If I use FB.LogInWithPublishPermissions (new List<string> () { "publish_actions" }, LoginCallBack); all the users are able to save their score but can not see friends scores.
    If I use FB.LogInWithPublishPermissions (new List<string> () { "publish_actions" , "user_friends" }, LoginCallBack); the application crash on loggin. Same with FB.LogInWithPublishPermissions (new List<string> () { "publish_actions, user_friends" }, LoginCallBack);
    On the editor applications works but no friends are listed.
    does someone has a solution?
     
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    I believe you have to get the user_friends through the
    LogInWithReadPermissions

    First. Then you do the publish permissions at another time (usually when you actually need it)
     
  13. javier-mazzurco

    javier-mazzurco

    Joined:
    Jan 22, 2016
    Posts:
    6
  14. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    hmm...I don't use the FB leaderboard system. We have one with Playfab and we can log into it using FB credentials and get a leaderboard that includes friends.

    How are you testing friends? Are you using testing accounts where you made them friends and did a high score or you using other real people that are developers on the project and testing with them?
     
  15. javier-mazzurco

    javier-mazzurco

    Joined:
    Jan 22, 2016
    Posts:
    6
    I'm testing with my son's account.
    So finally it works. It seems that once a user installs the applications and give some permission, those will never change, even if he uninstall and re install a new version of the applications asking for more permission.
    You need to go to facebook and manually delete the game in the user configuration.
    Thanks for your help!
     
  16. cstlmode

    cstlmode

    Joined:
    Dec 2, 2014
    Posts:
    88
    @javier-mazzurco hello ,
    what kind of login permission you have used , i have publish actions cleared from facebook , i used this to login
    FB.LogInWithPublishPermissions(new List<string>() { "publish_actions,email,public_profile,user_friends" }, LoginCallBack);
    and still can't see my real FB friends score
    please help
     
  17. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    I don't believe publishpermissions lets you get the friends permission. You'll need to use the LoginWithReadPermissions first. Then later you should ask for publish permissions if you need it.
     
  18. cstlmode

    cstlmode

    Joined:
    Dec 2, 2014
    Posts:
    88
    okey thank you ,
    can you please explain further what do you mean by later , do you mean when i want to overwrite a FB score for an example ,
     
  19. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Normally you only want to ask for publish permission when you need it. The first time you require it for example. Let's say you require publish permission to post a score, you should check if your user already granted that permission and if not, do the LoginWithPublishPermission to prompt for that permission. If they already granted it, you should be able to skip that and just publish the score.

    Facebook considers this best practice.
     
  20. cstlmode

    cstlmode

    Joined:
    Dec 2, 2014
    Posts:
    88
    i see what you mean , i tried a quick solution but i'm getting a difference permissions issue which is not granting me publish actions and email even though i asked for it ,
    here is the code , please have a look at it and tell me what you think about this
    Code (CSharp):
    1.    
    2.  
    3. public void FBlogin()
    4.     {
    5.         if (!FB.IsLoggedIn)
    6.  
    7.             FB.LogInWithReadPermissions(new List<string>() {"email","public_profile","user_friends" }, LoginCallBack);
    8.  
    9. }
    10. //login call back
    11. void LoginCallBack(IResult result)
    12.     {
    13.  
    14.         FB.API("/me/permissions", HttpMethod.GET, delegate (IGraphResult response) {
    15.  
    16.             Debug.Log("response" + response.RawResult);
    17.  
    18.             var perm = Json.Deserialize(response.RawResult) as Dictionary<string, object>;
    19.  
    20.             var permList = new List<object>();
    21.             permList = (List<object>)(perm["data"]);
    22.  
    23.                     foreach (object permissiom in permList)
    24.                     {
    25.  
    26.                       var Permission = (Dictionary<string, object>)permissiom;
    27.  
    28.                       if (Permission["permission"].ToString() == "publish_actions")
    29.                         {
    30.                             Debug.Log("publish_actions  granted !!");
    31.                         }
    32.                     else
    33.                         {
    34.                             Debug.Log("publish_actions not granted !!");
    35.                     FB.LogInWithPublishPermissions(new List<string>() { "publish_actions" });
    36.                 }
    37.                     }
    38.         });
    39. }

    Debug.log permissions result
    response{"data":[{"permission":"user_friends","status":"granted"},{"permission":"public_profile","status":"granted"}]}
     
  21. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    hmm...Well, the LogInWithReadPermissions looks like ours, so I'm going to assume it's correct.

    You could always run your queryString and see if it returns the email. I know we're getting it on our end for our game.

    The permission list isn't going to include the publish_action because you ask for that after you check which permissions are granted. Actually, you're asking for it in the loop right now which isn't correct. You should verify there is no publish_action permission and then prompt. Doing it in the loop will ask over and over again if the user doesn't grant it.
     
  22. cstlmode

    cstlmode

    Joined:
    Dec 2, 2014
    Posts:
    88
    thanx a lot Branthnann , it's working now
    the problem was the acces token , the default one does not have the permissions i was asking for this is why i was getting user friends and public profile only , the place where to get acces token with desired permissions is open graph API tool
    here is the link in case someone was in the same situation as mine
    https://developers.facebook.com/too...cores?flied=score,users.limit(7)&version=v2.9
     
  23. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    hmm...Well, with testing in the editor, I usually just use my access token under tools for my account. Or, there are also test user accounts which should also prompt for permissions.

    Glad you got it working though! FB docs aren't always maintained very well, so sometimes it can be confusing.
     
    cstlmode likes this.
  24. cstlmode

    cstlmode

    Joined:
    Dec 2, 2014
    Posts:
    88
    thanx a lot brathnann,
    i forgot to mention that the code i posted above is incorect as you noted, FB.LogInWithPublishPermissions(new List<string>() { "publish_actions" })
    will be called multiple times inside for, i have added a break, to that for loop to solve this issue ,
     
  25. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    80
    I know this thread id quite old, but is there anyway to can retrieve the list of all user's score in my app, not only friends? I cannot see the description on official FB pages, they just said that you can get friends' score using user_friends permission. However, based on my test, without user_friends permission, FB responses only friends's score in my app.

    Any help? If this cannot be reached, i think I have to use other 3rd party leaderboard, any recommendation the reliable one?
     
  26. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    If you mean, get a leaderboard for all users, FB to my knowledge doesn't support that.

    Depending on how many leaderboards you need, there are two we use at the moment. Braincloud allows unlimited leaderboards on an app, but cost $30 a month once live.

    Playfab is free, but has limitations on its free tier which may be enough for you, but looks like it would get expensive quickly if you went beyond the free tier limits.

    There is also Gamesparks, but I haven't used that yet in any projects.

    Google offers Firebase which also has a free tier with limits as well.
     
  27. DreamersGraphics

    DreamersGraphics

    Joined:
    Jan 5, 2018
    Posts:
    3
    Hi, I am new to game development and I would like to know how do I set a score and post on Facebook to create a leader board for friends only.

    Thanks.
     
  28. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    If you are using Facebook for your scoring, they have examples on their developer site for how to do scoring as well as a project Friend Smash that I believe has examples. Probably better you dig into those.
     
  29. DreamersGraphics

    DreamersGraphics

    Joined:
    Jan 5, 2018
    Posts:
    3
    Thanks Brathnann

    I managed to post my score and my friend score only when I signed him as a tester. when he is not a tester he cannot post his score and my app is already live but I have submitted it for review for the app center. Is that happening because the app is under App center review or there is another reason?
     
  30. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Yeah, if you haven't been approved, many of the features don't work. Once they approve your app, you should see what permissions were approved with it.

    I think scores requires publish permission if I recall, but I am not sure. We use a different service for leaderboard stuff.
     
  31. DreamersGraphics

    DreamersGraphics

    Joined:
    Jan 5, 2018
    Posts:
    3
    Thank you :)