I’m trying to use the facebook sdk to get user scores. I’ve been following a couple tutorials on youtube for integrating into unity. When i try to get the score data it returns an html error page instead of json as I am expecting.
Here’s the code for my facebook manager. The relevant code is in the queryScores & ScoresCallback toward the bottom:
public class fbManager : MonoBehaviour {
public GameObject DialogLoggedIn;
public GameObject DialogLoggedOut;
public GameObject DialogProfilePic;
public GameObject DialogShare;
public GameObject DialogInvite;
public GameObject DialogSetScore;
public GameObject DialogQueryScores;
public GameObject DialogDebugScores;
public Text Username;
public Text ScoresDebug;
public Text txtStatus;
List<string> perms = new List<string>(){"public_profile", "email", "user_friends"};
void Awake (){
FaceBookManager.Instance.InitFB();
FBMenus(FB.IsLoggedIn);
/**if (!FB.IsInitialized) {
// Initialize the Facebook SDK
FB.Init(InitCallback, OnHideUnity);
} else {
// Already initialized, signal an app activation App Event
FB.ActivateApp();
}
Debug.Log(FaceBookManager.Instance.IsLoggedIn); **/
}
private void InitCallback (){
if (FB.IsInitialized) {
// Signal an app activation App Event
FB.ActivateApp();
// Continue with Facebook SDK
// ...
} else {
Debug.Log("Failed to Initialize the Facebook SDK");
txtStatus.text = "Failed to Initialize the Facebook SDK";
}
FBMenus(FB.IsLoggedIn);
}
private void OnHideUnity (bool isGameShown){
if (!isGameShown) {
// Pause the game - we will need to hide
Time.timeScale = 0;
} else {
// Resume the game - we're getting focus again
Time.timeScale = 1;
}
}
public void FBLogin(){
FB.LogInWithReadPermissions(perms, AuthCallback);
}
private void AuthCallback (ILoginResult result) {
if (FB.IsLoggedIn) {
// AccessToken class will have session details
var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
// Print current access token's User ID
Debug.Log(aToken.UserId);
// Print current access token's granted permissions
foreach (string perm in aToken.Permissions) {
Debug.Log(perm);
}
FaceBookManager.Instance.IsLoggedIn = true;
FaceBookManager.Instance.GetProfile();
} else {
Debug.Log("User cancelled login");
txtStatus.text = "User cancelled login";
}
FBMenus(FB.IsLoggedIn);
}
void FBMenus(bool isLoggedIn){
if(isLoggedIn){
DialogLoggedIn.SetActive(true);
DialogLoggedOut.SetActive(false);
DialogShare.SetActive(true);
DialogInvite.SetActive(true);
DialogSetScore.SetActive(true);
DialogQueryScores.SetActive(true);
DialogDebugScores.SetActive(true);
QueryScore();
//FB.API("/me?fields=first_name", HttpMethod.GET,DisplayUsername);
//FB.API("me/picture?type=square&height=128&width=128", HttpMethod.GET, DisplayProfilePic);
if(FaceBookManager.Instance.ProfileName != null){
Username.text = "Hi, " + FaceBookManager.Instance.ProfileName;
} else {
StartCoroutine("WaitForProfileName");
}
if(FaceBookManager.Instance.ProfilePic != null){
Image ProfilePic = DialogProfilePic.GetComponent<Image>();
ProfilePic.sprite = FaceBookManager.Instance.ProfilePic;
} else {
StartCoroutine("WaitForProfilePic");
}
} else {
DialogLoggedIn.SetActive(false);
DialogLoggedOut.SetActive(true);
DialogShare.SetActive(false);
DialogInvite.SetActive(false);
DialogSetScore.SetActive(false);
DialogQueryScores.SetActive(false);
DialogDebugScores.SetActive(false);
}
}
IEnumerator WaitForProfileName(){
while(FaceBookManager.Instance.ProfileName == null){
yield return null;
}
FBMenus(FaceBookManager.Instance.IsLoggedIn);
}
IEnumerator WaitForProfilePic(){
while(FaceBookManager.Instance.ProfilePic == null){
yield return null;
}
FBMenus(FaceBookManager.Instance.IsLoggedIn);
}
void DisplayUsername(IResult result){
if(result.Error == null){
Username.text = "Hi there, " + result.ResultDictionary["first_name"];
} else {
Debug.Log(result.Error);
}
}
void DisplayProfilePic(IGraphResult result){
if(result.Texture != null){
Image ProfilePic = DialogProfilePic.GetComponent<Image>();
ProfilePic.sprite = Sprite.Create(result.Texture, new Rect(0,0,128,128), new Vector2());
}
}
public void Share(){
FaceBookManager.Instance.Share();
}
public void Invite(){
FaceBookManager.Instance.Invite();
}
public void QueryScore(){
FB.API("/app/scores?fields=score, user.limit(30)", HttpMethod.GET, ScoresCallback);
}
private void ScoresCallback(IGraphResult result){
/**
IDictionary<string, object>data = result.ResultDictionary;
List<object> scoreList = (List<object>)data["data"];
foreach(object obj in scoreList){
var entry = (Dictionary<string, object>)obj;
var User = (Dictionary<string, object>)entry["user"];
Debug.Log(User["name"].ToString() + " , " + entry["score"].ToString());
ScoresDebug.text = User["name"].ToString() + " , " + entry["score"].ToString();
} **/
Debug.Log("Scores callback: " + result.RawResult);
ScoresDebug.text = result.RawResult.ToString();
//Debug.Log(gameManager.bestTime);
//ScoresDebug.text = Username.text + " : " + PlayerPrefs.GetFloat("bestTime").ToString();
//ScoresDebug.text = FaceBookManager.Instance.ProfileName + " : " + PlayerPrefs.GetFloat("bestTime").ToString();
}
public void SetScore(){
}
}
Here’s what it returns in the console:
Scores callback:
<title>Facebook | Error</title>
<meta charset="utf-8">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="cache-control" content="no-store">
<meta http-equiv="cache-control" content="max-age=0">
<meta http-equiv="expires" content="-1">
<meta http-equiv="pragma" content="no-cache">
<meta name="robots" content="noindex,nofollow">
<style>
html, body {
color: #141823;
background-color: #e9eaed;
font-family: Helvetica, Lucida Grande, Arial,
Tahoma, Verdana, sans-serif;
margin: 0;
padding: 0;
text-align: center;
}
#header {
height: 30px;
padding-bottom: 10px;
padding-top: 10px;
text-align: center;
}
#icon {
width: 30px;
}
h1 {
font-size: 18px;
}
p {
font-size: 13px;
}
#footer {
border-top: 1px solid #ddd;
color: #9197a3;
font-size: 12px;
padding: 5px 8px 6px 0;
}
<div id="header">
<a href="//www.facebook.com/">
<img id="icon" src="//static.xx.fbcdn.net/images/logos/facebook_2x.png" />
</a>
</div>
<div id="core">
<h1 id="sorry">Sorry, something went wrong.</h1>
<p id="promise">
We're working on it and we'll get it fixed as soon as we can.
</p>
<p id="back-link">
<a id="back" href="//www.facebook.com/">Go Back</a>
</p>
<div id="footer">
Facebook
<span id="copyright">
© 2017
</span>
<span id="help-link">
·
<a id="help" href="//www.facebook.com/help/">Help Center</a>
</span>
</div>
</div>
<script>
document.getElementById('back').onclick = function() {
if (history.length > 1) {
history.back();
return false;
}
};
// Adjust the display based on the window size
if (window.innerHeight < 80 || window.innerWidth < 80) {
// Blank if window is too small
document.body.style.display = 'none';
};
if (window.innerWidth < 200 || window.innerHeight < 150) {
document.getElementById('back-link').style.display = 'none';
document.getElementById('help-link').style.display = 'none';
};
if (window.innerWidth < 200) {
document.getElementById('sorry').style.fontSize = '16px';
};
if (window.innerWidth < 150) {
document.getElementById('promise').style.display = 'none';
};
if (window.innerHeight < 150) {
document.getElementById('sorry').style.margin = '4px 0 0 0';
document.getElementById('sorry').style.fontSize = '14px';
document.getElementById('promise').style.display = 'none';
};
</script>
UnityEngine.Debug:Log(Object)
fbManager:ScoresCallback(IGraphResult) (at Assets/Scripts/fbManager.cs:182)
Facebook.Unity.c__Iterator0:MoveNext()
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
I’ve been following a tutorial by ‘greyzoned’ on youtube for the scores api, and i can’t figure out what i’m doing wrong. Any help is greatly appreciated!