Login with Facebook and invite friends to game using photon

Hey, I am using Pun for my multiplayer shooting game in which only two players can join the and it works perfectly. Now I want to implement facebook Login system in photon and allow the player to invite his facebook friends to his game. in my research i found this link. https://doc.photonengine.com/en-us/pun/current/demos-and-tutorials/pun-and-facebook-custom-authentication and implmented which works fine. Now I want to invite friends to my game but cant find anything related to this. You have mentioned a link in most of the topics which is broke or been removed. http://doc.exitgames.com/en/pun/current/tutorials/pun-and-facebook-custom-authentication

in other tutorials/discussions you've mentioned the code to get friend list.
void OnEnable(){
	StartCoroutine("UpdateFriendsOnServer");
}

void OnDisable(){
	StopCoroutine("UpdateFriendsOnServer");
}


IEnumerator UpdateFriendsOnServer()
{
	while (true) {
		if (_friendList != null) 
		{
			string[] friends = _friendList.ToArray ();
			PhotonNetwork.FindFriends (friends);
		}
		yield return new WaitForSeconds(friendsCheckPeriod);
	}
}

override void OnUpdatedFriendList () {
	Debug.Log ("OnUpdatedFriendList");
	if (PhotonNetwork.Friends != null) 
	{
		foreach(FriendInfo friend in PhotonNetwork.Friends) 
		{
			Debug.Log ("Friend name: " + friend.Name + " Online: " + friend.IsOnline);
		}
	}
}
I just don't understand that how will i get and initialize the _friendList list.





please help me solve my problem.

Answers

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2017
    Hi @SaadSalman,

    Thank you for choosing Photon!

    Do not start duplicate forum topics.
    To get Facebook friends list you need to download and use the <a href="https://developers.facebook.com/docs/unity/">Facebook Unity SDK</a>.

    Tutorial:
    - video: https://youtu.be/QVuVwTwKjxE?t=9m58s
    - text: http://resocoder.com/2017/08/04/facebook-unity-sdk-tutorial-share-invite-get-friends-code/
  • thank you for your quick response, you saved my day but now the only problem is i'm unable to get Debug
    Android Hash key. I followed below link but still not getting.
    https://answers.unity.com/questions/573199/debug-android-hash-key-facebook-sdk.html

  • @JohnTube
    JohnTube wrote: »
    Hi @SaadSalman,

    Thank you for choosing Photon!

    Do not start duplicate forum topics.
    To get Facebook friends list you need to download and use the <a href="https://developers.facebook.com/docs/unity/">Facebook Unity SDK</a>.

    Tutorial:
    - video: https://youtu.be/QVuVwTwKjxE?t=9m58s
    - text: http://resocoder.com/2017/08/04/facebook-unity-sdk-tutorial-share-invite-get-friends-code/

    in this, FB.Mobile.AppInvite is removed any other method to do this?
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2020
    Hi @anmol07goyal,

    Thank you for choosing Photon!

    That video was shared here on 2017.
    Facebook has deprecated AppInvites since.
    You need to find out replacement of AppInvites feature either using facebook itself or other third party services.
    Here is an example of what search results for "facebook appinvites replacement" yielded: https://stackoverflow.com/questions/47196972/facebook-app-invites-is-deprecated
  • hey, I tried to do some changes in the GetFriendsPlayingThisGame() function:
    public void GetFriendsPlayingThisGame()
    {
    	string query = "/me/friends";
    	FB.API(query, HttpMethod.GET, result =>
    	{
    		var dictionary = (Dictionary<string, object>)Facebook.MiniJSON.Json.Deserialize(result.RawResult);
    		friendList = (List<object>) dictionary["data"];
    		yValue = 290;
    
    		text.text = string.Empty;
    		if (friendList.Count != 0)
    		{
    			foreach (var dict in friendList)
    			{
    				// text.text += ((Dictionary<string, object>) dict)["name"];
    				FriendInfo friend = (FriendInfo)dict;
    				text.text += "online?  " + friend.IsOnline;
    				text.text += "user id: " + friend.UserId;
    				text.text += "name: " + friend.Name;
    					
    				GameObject friendGO = Instantiate(friendPrefab);
    				friendGO.transform.SetParent(fbFriendConnected.transform.GetChild(2).GetChild(2));
    				friendGO.transform.localPosition = new Vector3(0, yValue,0);
    				friendGO.transform.localScale = Vector3.one;
    				text.text = ((Dictionary<string, object>) dict)["name"].ToString();
    				yValue2 -= 120;
    					
    			}
    		}
    		else
    		{
    			// no friends available to play
    		}
    	});
    }
    

    but this part:
    FriendInfo friend = (FriendInfo)dict;
    text.text += "online? " + friend.IsOnline;
    text.text += "user id: " + friend.UserId;
    text.text += "name: " + friend.Name;

    is not working any reason or correction on what I'm doind wrong
  • JohnTube
    JohnTube ✭✭✭✭✭
    I think you are confusing Photon's FriendInfo with Facebook's FriendInfo.

    You can't convert from Dictionary<string, object> to Photon's FriendInfo.
    You should probably get the content of that dictionary or serialize it into another class.

    I think you need to find a newer tutorial.
  • anmol07goyal
    edited January 2021
    hey @JohnTube ,
    if you can please help me on how to check whether our facebook friend is online or not to play the game, I would be really thankful to you

    Or if anyone knows on how to do this please help me on this .....I'm really having a hard time in figuring this out

    For now I can only display the name of the player using this:
    public void GetFriendsPlayingThisGame()
    	{
    		string query = "/me/friends";
    		FB.API(query, HttpMethod.GET, result =>
    		{
    			var dictionary = (Dictionary<string, object>) Json.Deserialize(result.RawResult);
    			friendList = (List<object>) dictionary["data"];
    			connectionTxt.text = friendList.Count.ToString();
    			if (friendList.Count != 0)
    			{
    				foreach (var dict in friendList)
    				{
    					connectionTxt.text += ((Dictionary<string, object>) dict).Values;
    				}
    		});
    	}
    
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @anmol07goyal,

    I looked at a very old code of mine and here is what I have found, I did not retest this so I can't tell you if it still works or not.
    The idea is to know what the Facebook Graph API for get "/me/friends" returns exactly.
    You could print/log result.RawResult understand its JSON structure and then parse it accordingly.
    You can always search for more recent up to date code or help online via YouTube or stackoverflow or elsewhere.
    This is not a Photon topic.
    public void GetFacebookFriends(int limit)
        {
            FB.API(string.Format("/me?fields=friends.limit({0}).fields(id)", limit), HttpMethod.GET, OnFriendsList);
        }
    
        private void OnFriendsList(IGraphResult result)
        {
            try
            {
                Dictionary<string, object> dict = result.ResultDictionary as Dictionary<string, object>;
                object temp;
                List<object> friends;
                List<string> friendsIds;
                if (dict.TryGetValue("friends", out temp))
                {
                    dict = temp as Dictionary<string, object>;
                    if (dict.TryGetValue("data", out temp))
                    {
                        friends = temp as List<object>;
                        friendsIds = new List<string>(friends.Count);
                        for (int i = 0; i < friends.Count; i++)
                        {
                            var friendDict = friends[i] as Dictionary<string, object>;
                            var friend = new Dictionary<string, string>();
                            friend["id"] = friendDict["id"] as string;
                            friend["first_name"] = friendDict["first_name"] as string;
                            friendsIds.Add(friend["id"]);
                            friends.Add(friend);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogException(ex, this);
            }
        }