public IEnumerator Launch() { int cacheValidity = RequestKindCacheValidity(); CachedString cache = new CachedString(this.queryStr); if (cacheValidity > 0 && cache.IsValid()) { Debug.Log("*** Using cache result for " + queryStr + " ; remaining validity: " + cache.RemainingValidity()); this.result = cache.GetString(); } else { string host = Noco.Configuration.baseUrl; string urlStr = String.Format("https://{0}/1.1{1}", host, this.queryStr); UnityWebRequest request = null; request = new UnityWebRequest(); request.url = urlStr; request.downloadHandler = new DownloadHandlerBuffer(); request.SetRequestHeader("Authorization", "Basic " + this.api.oauthAccessToken.authorizationString()); Debug.Log("--- Fetching: " + request.url); yield return(request.Send()); this.result = request.downloadHandler.text; if (this.result != null && cacheValidity > 0 && this.result.IndexOf("\"error\"") == -1) { cache.Save(this.result, cacheValidity); } } if (this.result != null) { Parse(); } }
public IEnumerator LoadArchivedAccessToken() { CachedString cache = new CachedString("NocoAccessToken"); string tokenDescription = cache.GetString(); if (tokenDescription != null) { AccessTokenDescriptor previousTokenDescriptor = JsonUtility.FromJson <AccessTokenDescriptor> (tokenDescription); if (cache.IsValid()) { if (debugAuthentification) { Debug.Log("Access token still valid for " + cache.RemainingValidity() + " seconds"); } this.oauthAccessToken = previousTokenDescriptor; } else if (previousTokenDescriptor != null) { if (debugAuthentification) { Debug.Log("Trying to use refresh token after access token lapsing " + tokenDescription); } NocoOAuthAccessTokenRequest request = new NocoOAuthAccessTokenRequest(this.clientId, this.clientSecret); yield return(request.FetchAccessTokenFromRefreshToken(previousTokenDescriptor.refresh_token)); if (debugAuthentification) { Debug.Log("Refresh token call result:" + request.result); } this.oauthAccessToken = request.oauthAccessToken; if (IsAuthenticated()) { if (debugAuthentification) { Debug.Log("Refresh token succed: new access token found: " + this.oauthAccessToken); } this.oauthAccessToken.Save(); } } } }