我知道这是一个愚蠢的问题但是如何在用户登录后轻松保存用户访问令牌?
以下是我的代码的一部分,由按钮触发:
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 :(得分:2)
您可以通过创建一个自定义可序列化类来保存令牌,该类包含Facebook AccessToken
类中的所有变量。此自定义类应包含一个可用于将其数据转换为Facebook AccessToken
类的函数。
然后,您可以将该自定义类保存并加载为Json。
从this帖子中抓取DataSaver
课程,以简化保存和加载数据。我们会调用自定义类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;
不知道是否可以应用它但是它似乎没有问题地编译。
删除强>:
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;
}
}