Sunday, 1 January 2017

Hoe to get user data from facebook unity CSharp



Here is script for getting user data like first_name, last_name, gender, email and photo from facebook

you can also get friend data from this...

Here is link to get Unity facebook SDK and more detail process :-
 https://developers.facebook.com/docs/unity/gettingstarted


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Facebook.Unity;

public class FBLoginScript : MonoBehaviour {

public Image photo_base;
public Text id_base;
public Text fname_base;
public Text lname_base;
public Text gender_base;
public Text email_base;

void Awake () {
if (!FB.IsInitialized) {
FB.Init(InitCallback, OnHideUnity);
} else {
FB.ActivateApp();
}
}
private void InitCallback ()
{
if (FB.IsInitialized) {
FB.ActivateApp();
} else {
Debug.Log("Failed to Initialize the Facebook SDK");
}
}
private void OnHideUnity (bool isGameShown)
{
if (!isGameShown) {
Time.timeScale = 0;
} else {
Time.timeScale = 1;
}
}


List<string> perms = new List<string>(){"public_profile", "email", "user_friends"};


private void AuthCallback (ILoginResult result) {
if (FB.IsLoggedIn) {
var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
Debug.Log(aToken.UserId);
// Print current access token's granted permissions
foreach (string perm in aToken.Permissions) {
Debug.Log(perm);
}
} else {
Debug.Log("User cancelled login");
}
}

public void LogInButton()
{
FB.LogInWithReadPermissions(perms, AuthCallback);
}
public void LogOutButton()
{
FB.LogOut ();
Debug.Log("User logged out");
}
public void GetMailIDButton()
{
FB.API("/me?fields=id,first_name,last_name,gender,email", HttpMethod.GET, graphCallback);
FB.API("/me/picture?type=square&height=128&width=128", HttpMethod.GET, ProfilePhotoCallback);
//FB.API("/me/friends?fields=id,name", HttpMethod.GET, FriendInfoCallback);
FB.API("/me/friends?fields=id,first_name,picture.width(128).height(128)&limit=100", HttpMethod.GET, FriendInfoCallback);
//FB.API("/me?fields=email,friends.limit(10).fields(first_name,id)", HttpMethod.GET, FriendInfoCallback);

}
void FriendInfoCallback(IGraphResult result)
{
Debug.Log("hi= "+result.RawResult);
object dataList;
if (result.ResultDictionary.TryGetValue("data", out dataList))
{
var friendsList = (List<object>)dataList;
CacheFriends(friendsList);
Debug.Log("friendsList= "+Friends);

for(int i=0;i < Friends.Count;i++)
{
Debug.Log(Friends[i]);
}
}

}
public static List<object> Friends;
public static Dictionary<string, Texture> FriendImages = new Dictionary<string, Texture>();

private static void CacheFriends (List<object> newFriends)
{
if (Friends != null && Friends.Count > 0)
{
Friends.AddRange(newFriends);
}
else
{
Friends = newFriends;
}
}

void ProfilePhotoCallback(IGraphResult result)
{
photo_base.sprite = Sprite.Create(result.Texture, new Rect(0, 0, 128, 128), new Vector2(0, 0),125);
}
private void graphCallback(IGraphResult result)
{
Debug.Log("hi= "+result.RawResult);
string id;
string firstname;
string lastname;
string gender;
string email;
if (result.ResultDictionary.TryGetValue("id", out id))
{
id_base.text = "ID = " + id.ToString();
}
if (result.ResultDictionary.TryGetValue("first_name", out firstname))
{
fname_base.text = "firstname= "+firstname.ToString();
}
if (result.ResultDictionary.TryGetValue("last_name", out lastname))
{
lname_base.text = "lastname= "+lastname.ToString();
}
if (result.ResultDictionary.TryGetValue("gender", out gender))
{
gender_base.text = "gender= "+gender.ToString();
}
if (result.ResultDictionary.TryGetValue("email", out email))
{
email_base.text = "email= "+email.ToString();
}

}
}




You can get FB_user_Access token by clicking the "Find Access Token" then paste into field
Click Send Success 

Above testing process is for unity editor... you can login directly form your mobile ... 

1 comment: