Unity上的Facebook SDK无法获得除first_name之外的任何内容

时间:2014-09-05 13:12:26

标签: android facebook api sdk unity3d

所以这是我连接和获取值的代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Facebook;
using SmartLocalization;
public class mainMenuFacebook : MonoBehaviour {
    public string FBname;
    public string FBsurname;
    Dictionary<string, string>  profile = new Dictionary<string, string>();
    // Use this for initialization

void OnMouseDown()
{
    FB.Login("publish_actions,public_profile", LoginCallback);   // logine tıklama

}

void Awake()    {


    FB.Init(SetInit, OnHideUnity);  //facebook başlangıcı

}
private void SetInit()                                                                       
{                                                                                            

    if (FB.IsLoggedIn)                                                                       
    {                                                                                        
    //  Util.Log("Already logged in");                                                    
        OnLoggedIn();                                                                        
    }      

}                                                                                            

private void OnHideUnity(bool isGameShown)                                                   
{                                                                                            

    if (!isGameShown)                                                                        
    {                                                                                        
        // pause the game - we will need to hide                                             
        Time.timeScale = 0;                                                                  
    }                                                                                        
    else                                                                                     
    {                                                                                        
        // start the game back up - we're getting focus again                                
        Time.timeScale = 1;                                                                  
    }                                                                                        
}
void LoginCallback(FBResult result)                                                        
{                                                                                          
    Util.Log("LoginCallback");                                                          

    if (FB.IsLoggedIn)                                                                     
    {       gameObject.guiTexture.enabled = false;                                                                               
        OnLoggedIn();

    }                                                                                      
}                                                                                          

void OnLoggedIn()                                                                          
{                                                                                          
    FB.API("/me?fields=first_name,last_name,email", Facebook.HttpMethod.GET, APICallback);     // adını ve idyi çekiyoruz. 



}    
void APICallback(FBResult result)                                                                                              
{                                                                                                                              

    if (result.Error != null)                                                                                                  
    {                                                                                                                          

        // Let's just try again                                                                                                
    //  FB.API("/me?fields=id,first_name,last_name,email,friends.limit(100).fields(first_name,last_name,id)", Facebook.HttpMethod.GET, APICallback);      
        return;                                                                                                                
    }                                                                                                                          
    Debug.Log(result.Text);

    profile = Util.DeserializeJSONProfile(result.Text);

    FBname = profile["first_name"];
    FBsurname = profile["last_name"]; // **IT GIVES ERROR**
    Debug.Log(FBsurname + " " + FBname);

    //PlayerPrefs.SetString("surname",profile["last_name"]); 
    //PlayerPrefs.SetString("email",profile["email"]); 
    gameObject.guiTexture.enabled = false;
    GameObject.Find("Wellcome").guiText.enabled = true;
    GameObject.Find("Wellcome").guiText.text = LanguageManager.Instance.GetTextValue("menu.hosgeldin") + " <b><color=#ffa500ff>" + FBname + "</color></b>, <i>" + LanguageManager.Instance.GetTextValue("menu.cikis") +"</i>";
    PlayerPrefs.SetString("name",FBname);
}

}

当我只尝试获取first_name时一切正常。但我需要获取last_name和电子邮件。我想我不能序列化,因为当我尝试Debug.Log(profile.Count);它显示1.

我该如何解决?

给出的错误是:

    KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,System.String].get_Item (System.String key) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
mainMenuFacebook.APICallback (.FBResult result) (at Assets/Scripts/mainMenuFacebook.cs:84)
Facebook.AsyncRequestString+<Start>c__Iterator0.MoveNext ()

1 个答案:

答案 0 :(得分:0)

尝试一下:

public void OnMouseDown()
{
    List<string> permissions = new List<string>() { "public_profile", "email" };
    FB.LogInWithReadPermissions(permissions,AuthCallback);
    Debug.Log("Facebook Login");
}

在AuthCallback中:如果用户成功登录,请从FB api获取信息。

private void AuthCallback(ILoginResult result)
{
    if(FB.IsLoggedIn)
    {
        GetInfo();
    }
    else
    {
        Debug.Log("User cancelled login");
    }
}

FB API返回json结果,因此您需要FacebookUser类对其进行反序列化。

class FacebookUser
{
    public string id;
    public string first_name;
    public string last_name;
    public string email;
}

public void GetInfo()
{
    FB.API("/me?fields=id,first_name,last_name,email", HttpMethod.GET, result =>
    {
        if(result.Error != null)
        {
            Debug.Log("Result error");
        }

        var facebookUser = Newtonsoft.Json.JsonConvert.DeserializeObject<FacebookUser>(result.RawResult);

        Debug.Log(" facebook id - " + facebookUser.id);
        Debug.Log(" facebook first name - " + facebookUser.first_name);
        Debug.Log(" facebook last name - " + facebookUser.last_name);
        Debug.Log(" facebook email - " + facebookUser.email);
    });
}

注意:您应该具有Facebook的电子邮件许可才能访问它。  在Graph API Explorer

中检查