private AccessTokenResult RetrieveAccessToken(VerifierResult verifierResult) { if (verifierResult == null) { throw new ArgumentNullException("verifierResult"); } if (string.IsNullOrEmpty(verifierResult.OAuthToken)) { throw new ArgumentException("verifierResult.OAuthToken"); } if (string.IsNullOrEmpty(verifierResult.OAuthToken)) { throw new ArgumentException("verifierResult.OAuthVerifier"); } IRestResponse response; try { var request = new RestRequest("!api/1.0/oauth/access_token", Method.POST); var restClient = _restClientFactory.CreateRestClient(BaseUrl); // Retrieve the token secret from the cache :) string tokenSecret = null; if (_memoryCache != null && _memoryCache.Contains(verifierResult.OAuthToken)) { tokenSecret = (string)_memoryCache.Get(verifierResult.OAuthToken); _memoryCache.Remove(verifierResult.OAuthToken); } restClient.Authenticator = OAuth1Authenticator.ForAccessToken(_consumerKey, _consumerSecret, verifierResult.OAuthToken, tokenSecret, verifierResult.OAuthVerifier); response = restClient.Execute(request); } catch (Exception exception) { throw new AuthenticationException("Failed to convert Request Token to an Access Token, from BitBucket.", exception); } if (response == null || response.StatusCode != HttpStatusCode.OK) { throw new AuthenticationException( string.Format( "Failed to obtain an Access Token from " + Name + " OR the the response was not an HTTP Status 200 OK. Response Status: {0}. Response Description: {1}", response == null ? "-- null response --" : response.StatusCode.ToString(), response == null ? string.Empty : response.StatusDescription)); } var querystringParameters = HttpUtility.ParseQueryString(response.Content); return(new AccessTokenResult { AccessToken = querystringParameters[OAuthTokenKey], AccessTokenSecret = querystringParameters[OAuthTokenSecretKey] }); }
/// <summary> /// 获取缓存 /// </summary> /// <param name="key">关键字</param> /// <returns></returns> public static object GetObjectCache(string key) { object obj = null; if (objCache.Contains(key)) { obj = objCache.Get(key); } return(obj); }
protected TResult Get <TResult>(string partialKey, Func <TResult> getFunc) where TResult : class { var key = string.Format("{0}_{1}", CacheKeyBase, partialKey); var cached = _cache.Get(key); if (cached != null) { System.Diagnostics.Debug.WriteLine("Geocoding cache hit " + key); return((TResult)cached); } System.Diagnostics.Debug.WriteLine("Geocoding CACHE MISS " + key); var result = getFunc(); if (result != null) { _cache.Add(key, result, Policy); } return(result); }