/// <summary> /// Saves an access token at the selected PlayerPrefs. /// </summary> /// <param name="prefsName">PlayerPrefs where the token will be saved as JSON.</param> /// <param name="token">Access toekn to save.</param> private static void SetPrefsToken(string prefsName, AccessToken token) { StoredToken_JSON tokenJson = new StoredToken_JSON() { access_token = token.token, token_type = token.type, expiration = token.expiration.ToBinary().ToString(), scope = token.scope }; PlayerPrefs.SetString(prefsName, JsonUtility.ToJson(tokenJson)); }
/// <summary> /// Retrieves an access token from the selected PlayerPrefs. /// </summary> /// <param name="prefsName">PlayerPrefs where the token is stored.</param> /// <returns>Returns an access token converted from JSON, or 'null' if the PlayerPrefs is invalid.</returns> private static AccessToken GetPrefsToken(string prefsName) { string tokenString = PlayerPrefs.GetString(prefsName); if (string.IsNullOrEmpty(tokenString)) { return(null); } StoredToken_JSON tokenJson = JsonUtility.FromJson <StoredToken_JSON>(tokenString); long temp; try { temp = Convert.ToInt64(tokenJson.expiration); } catch { temp = 0; } DateTime expiration = temp != 0 ? DateTime.FromBinary(temp) : DateTime.Now.AddSeconds(-1); return(new AccessToken(tokenJson.access_token, tokenJson.token_type, expiration, tokenJson.scope)); }