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]
            });
        }
示例#2
0
 /// <summary>
 ///删除缓存
 /// </summary>
 /// <param name="key">关键字</param>
 public static void RemoveObjectCache(string key)
 {
     if (objCache.Contains(key))
     {
         objCache.Remove(key);
     }
 }