【问题标题】:Facebook SDK on Unity cant get anything except first_nameUnity 上的 Facebook SDK 无法获取除 first_name 之外的任何内容
【发布时间】:2014-09-05 13:12:25
【问题描述】:

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

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 时,一切都很好。但我也需要获取姓氏和电子邮件。我想我不能序列化,因为当我尝试 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 ()

【问题讨论】:

  • 您使用的是哪个 FB SDK?
  • 我使用的是 Unity 5.1
  • 同样的问题。有没有想过?

标签: android facebook api sdk unity3d


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    相关资源
    最近更新 更多