2017-05-16 43 views
1

我知道這是一個愚蠢的問題,但如何在用戶登錄後輕鬆保存用戶訪問令牌?保存並加載Facebook用戶訪問令牌

這裏是我的代碼的一部分由一個按鈕觸發:

public class FacebookLogin : MonoBehaviour { 


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; 
    } 
} 

public void FBLogin() 
{ 
    List<string> perms = new List<string>() { "public_profile", "email", "user_friends" }; 
    FB.LogInWithReadPermissions(perms, AuthCallback); 
} 

private void AuthCallback(ILoginResult result) 
{ 
    if (FB.IsLoggedIn) 
    { 
     var aToken = Facebook.Unity.AccessToken.CurrentAccessToken; 

     FB.API("/me?fields=first_name", HttpMethod.GET, getName); 
     FB.API("/me/picture?type=square&height=128&width=128", HttpMethod.GET, getProfilePic); 
     SceneManager.LoadScene("Main_Menu"); 
    } 
    else 
    { 
     Debug.Log("User cancelled login"); 
    } 
} 

private void getName(IResult result) 
{ 
    LocalDataBase.Name = result.ResultDictionary["first_name"].ToString(); 
} 

private void getProfilePic(IGraphResult result) 
{ 
    LocalDataBase.profilePicture = result; 
} 

}

預先感謝您所有的答案。

+0

我不知道,但我認爲這是不允許的..需要一些研究.. –

+0

的我想要保存的令牌是 var aToken = Facebook.Unity.AccessToken.CurrentAccessToken; –

+0

這是一個Facebook.Unity.AccesToken。我的問題是不可syncronizable,所以我無法正常保存它。 –

回答

2

您可以通過製作一個自定義的可序列化類來包裝Facebook的AccessToken類中的所有變量來保存令牌。這個自定義類應該包含一個函數,可以用它將數據轉換爲Facebook的AccessToken類。

然後,您可以將該自定義類保存並加載爲Json。

抓取DataSaver類從this發佈,以簡化保存和加載數據。我們會打電話給我們的定製類FaceBookToken

您的Facebook令牌:

var aToken = Facebook.Unity.AccessToken.CurrentAccessToken; 

保存

FaceBookToken faceBookToken = FaceBookToken.createFaceBookToken(aToken); 
DataSaver.saveData(faceBookToken, "FB_Token"); 

負載

FaceBookToken loadedFaceBookToken = DataSaver.loadData<FaceBookToken>("FB_Token"); 
//AccessToken loadedToken = loadedFaceBookToken.toAccessToken(); //OR 
AccessToken loadedToken = loadedFaceBookToken; 

應用加載的數據

Facebook.Unity.AccessToken.CurrentAccessToken = loadedToken; 

不知道將有可能或allowd但它似乎沒有問題彙編。

刪除

DataSaver.deleteData("FB_Token"); 

FaceBookToken自定義類:

[Serializable] 
public class FaceBookToken 
{ 
    public static AccessToken CurrentAccessToken; 
    public DateTime ExpirationTime; 
    public DateTime? LastRefresh; 
    public IEnumerable<string> Permissions; 
    public string TokenString; 
    public string UserId; 

    //Function that let's you easily create new instance of FaceBookToken from AccessToken 
    public static FaceBookToken createFaceBookToken(AccessToken currentToken) 
    { 
     FaceBookToken faceBookToken = new FaceBookToken(); 
     FaceBookToken.CurrentAccessToken = AccessToken.CurrentAccessToken; 
     faceBookToken.ExpirationTime = currentToken.ExpirationTime; 
     faceBookToken.LastRefresh = currentToken.LastRefresh; 
     faceBookToken.Permissions = currentToken.Permissions; 
     faceBookToken.TokenString = currentToken.TokenString; 
     faceBookToken.UserId = currentToken.UserId; 
     return faceBookToken; 
    } 

    //Converts our custom FaceBookToken to AccessToken 
    public AccessToken toAccessToken() 
    { 
     AccessToken loadedToken = new AccessToken(TokenString, UserId, 
      ExpirationTime, Permissions, LastRefresh); 
     return loadedToken; 
    } 

    //Converts our custom FaceBookToken to AccessToken(Implicit Cast) 
    public static implicit operator AccessToken(FaceBookToken currentToken) 
    { 
     AccessToken loadedToken = new AccessToken(currentToken.TokenString, currentToken.UserId, 
      currentToken.ExpirationTime, currentToken.Permissions, currentToken.LastRefresh); 
     return loadedToken; 
    } 
}