Hello Friends,
I am working on a project in which I am using Facebook SDK. I want to fetch the first name, last name, gender, email, age and location(region) of the user logged in.
Though I got the profile pic, first name, last name, and gender, I am unable to fetch email, age, and location. Check the below script and let me know what’s wrong with it. Genuine answers are appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;
using UnityEngine.UI;
public class fbscript : MonoBehaviour {
public GameObject DialogLoggedIn;
public GameObject DialogLoggedOut;
public Text Dialogusername;
public Image DialogProfilePic;
public Text DialogGender;
public Text DialogBirthday;
public Text DialogLocation;
public Text DialogAge;
void Awake()
{
FB.Init (SetInit, onHideUnity);
}
void SetInit()
{
if (FB.IsLoggedIn)
Debug.Log ("fb is logged in SetInit method");
else {
Debug.Log ("fb is not logged in SetInit method");
}
dealWithFbMenus (FB.IsLoggedIn);
}
void onHideUnity(bool isGameShown)
{
if (!isGameShown)
Time.timeScale = 0;
else {
Time.timeScale = 1;
}
}
public void FBLogin()
{
List<string> permissions = new List<string>();
permissions.Add ("public_profile");
permissions.Add ("email");
permissions.Add ("user_location");
FB.LogInWithReadPermissions (permissions, AuthCallBack);
}
void AuthCallBack(IResult result)
{
if (result.Error != null) {
// Debug.Log (result.Error);
// FB.LogInWithReadPermissions (permissions, AuthCallBack);
}
else {
if (FB.IsLoggedIn)
{
Debug.Log (“fb is logged in AuthCallBack method”);
// Debug.Log (result.RawResult);
}
else
{
Debug.Log (“fb is not logged in AuthCallBack method”);
}
dealWithFbMenus (FB.IsLoggedIn);
}
}
void dealWithFbMenus(bool isLoggedIn)
{
if (isLoggedIn) {
DialogLoggedIn.SetActive (true);
DialogLoggedOut.SetActive (false);
FB.API ("/me?fields=name,gender", HttpMethod.GET, DisplayUserName);
FB.API ("/me/picture?type=large", HttpMethod.GET, DisplayProfilePic);
FB.API ("/me?fields=user_birthday,location,age", HttpMethod.GET, DisplayOtherDetails);
} else {
DialogLoggedIn.SetActive (false);
DialogLoggedOut.SetActive (true);
}
}
void DisplayUserName(IResult result)
{
Text username = Dialogusername;
Text gender = DialogGender;
if (result.Error == null) {
username.text = "" + result.ResultDictionary ["name"];
gender.text = "" + result.ResultDictionary ["gender"];
} else {
Debug.Log (result.Error);
}
}
void DisplayProfilePic(IGraphResult result){
Image profilePic;
if (result.Texture != null) {
profilePic = DialogProfilePic;
profilePic.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 200, 199), new Vector2 ());
}
}
void DisplayOtherDetails(IGraphResult result){
Image profilePic;
Text bday = DialogBirthday;
Text loc = DialogLocation;
Text age = DialogAge;
if (result.Error == null) {
bday.text = "" + result.ResultDictionary ["user_birthday"];
bday.text = "" + result.ResultDictionary ["location"];
bday.text = "" + result.ResultDictionary ["age"];
}
}
}
//
Thanks,