public void RequestForAccessToken()
        {
            QueryParameters oauthParameters = new QueryParameters();
            string          signature       = "";
            OAuthHelper     oauthHelper     = new OAuthHelper();

            ////1. Generate Signature
            oauthParameters.Add("oauth_consumer_key", provider.Consumerkey);
            oauthParameters.Add("oauth_token", ConnectionToken.AuthorizationToken);
            oauthParameters.Add("oauth_signature_method", provider.SignatureMethod.ToString());
            oauthParameters.Add("oauth_timestamp", oauthHelper.GenerateTimeStamp());
            oauthParameters.Add("oauth_nonce", oauthHelper.GenerateNonce());
            oauthParameters.Add("oauth_version", "1.0");
            oauthParameters.Add("oauth_verifier", ConnectionToken.OauthVerifier);
            signature = oauthHelper.GenerateSignature(new Uri(provider.AccessTokenEndpoint), oauthParameters, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, provider.TransportName, ConnectionToken.TokenSecret);
            oauthParameters.Add("oauth_signature", signature);

            //2. Notify Consumer (if applicable)
            BeforeRequestingAccessToken(oauthParameters); // hook called

            //3.Connect and obtain Token
            string targetUrl = provider.AccessTokenEndpoint + "?" +
                               oauthHelper.GetAuthorizationUrlParameters(oauthParameters);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl);

            request.Method = provider.TransportName.ToString();
            //request.Headers.Add("Authorization", oauthHelper.GetAuthorizationHeader(oauthParameters));
            request.ContentLength = 0;
            string response = "";

            try
            {
                logger.Debug("Requesting Access Token at " + provider.AccessTokenEndpoint);
                using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
                    using (Stream responseStream = webResponse.GetResponseStream())
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            response = reader.ReadToEnd();
                            var responseCollection = Utility.GetQuerystringParameters(response);
                            HandleAccessTokenResponse(responseCollection);
                            AfterGettingAccessToken(responseCollection, ConnectionToken);
                        }
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.AccessTokenRequestError(provider.AccessTokenEndpoint, oauthParameters), ex);
                throw new OAuthException(ErrorMessages.AccessTokenRequestError(provider.AccessTokenEndpoint, oauthParameters), ex);
            }
        }
        public void RequestForAccessToken()
        {
            QueryParameters oauthParameters = new QueryParameters();
            string          signature       = "";
            OAuthHelper     oauthHelper     = new OAuthHelper();

            ////1. Generate Signature
            oauthParameters.Add("oauth_consumer_key", provider.Consumerkey);
            oauthParameters.Add("oauth_token", Utility.UrlEncode(ConnectionToken.RequestToken));
            oauthParameters.Add("oauth_signature_method", provider.SignatureMethod.ToString());
            oauthParameters.Add("oauth_timestamp", oauthHelper.GenerateTimeStamp());
            oauthParameters.Add("oauth_nonce", oauthHelper.GenerateNonce());
            oauthParameters.Add("oauth_version", "1.0");
            BeforeRequestingAccessToken(oauthParameters); // hook called
            signature = oauthHelper.GenerateSignature(new Uri(provider.AccessTokenEndpoint), oauthParameters, provider.Consumerkey, provider.Consumersecret, provider.SignatureMethod, provider.TransportName, string.Empty);
            oauthParameters.Add("oauth_signature", Utility.UrlEncode(signature));

            //2. Notify Consumer (if applicable)
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(provider.AccessTokenEndpoint + "?" + oauthParameters.ToString().Replace("HMACSHA1", "HMAC-SHA1"));

            request.Method        = "GET";// always get irrespective of provider.TransportName.ToString();
            request.ContentLength = 0;
            string response = "";

            try
            {
                logger.Debug("Requesting Access Token at: " + request.RequestUri + Environment.NewLine + "Request Parameters: " + oauthParameters.ToString());
                using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
                    using (Stream responseStream = webResponse.GetResponseStream())
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            response = reader.ReadToEnd();
                            HandleAccessTokenResponse(Utility.GetQuerystringParameters(response));
                        }
            }
            catch (Exception ex)
            {
                logger.Debug(ErrorMessages.AccessTokenRequestError(request.RequestUri.ToString(), oauthParameters), ex);
                throw new OAuthException(ErrorMessages.AccessTokenRequestError(request.RequestUri.ToString(), oauthParameters), ex);
            }
        }
        public void RequestForAccessToken(TRANSPORT_METHOD method = TRANSPORT_METHOD.GET)
        {
            if (method != TRANSPORT_METHOD.GET && method != TRANSPORT_METHOD.POST)
            {
                throw new OAuthException("Invalid Verb used for requesting AccessToken. Supported verbs are GET/POST.");
            }

            UriBuilder ub = new UriBuilder(provider.AccessTokenEndpoint);
            //logger.LogAuthorizationRequest(ub.ToString());
            HttpWebRequest request;

            if (method == TRANSPORT_METHOD.POST)
            {
                request = (HttpWebRequest)WebRequest.Create(ub.ToString());
                string postData = "code=" + ConnectionToken.Code;
                postData += ("&client_id=" + provider.Consumerkey);
                postData += ("&client_secret=" + provider.Consumersecret);
                postData += ("&redirect_uri=" + ConnectionToken.ProviderCallbackUrl);
                postData += ("&grant_type=authorization_code");

                request.Method = "POST";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
                // Get the response.
            }
            else
            {
                ub.SetQueryparameter("client_id", provider.Consumerkey);
                ub.SetQueryparameter("client_secret", provider.Consumersecret);
                ub.SetQueryparameter("code", ConnectionToken.Code);
                ub.SetQueryparameter("redirect_uri", ConnectionToken.ProviderCallbackUrl);
                ub.SetQueryparameter("grant_type", "authorization_code");

                request        = (HttpWebRequest)WebRequest.Create(ub.ToString());
                request.Method = "POST";
            }


            try
            {
                logger.Debug("Requesting Access Token at " + ub.ToString());
                using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse())
                    using (Stream responseStream = webResponse.GetResponseStream())
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            string authToken = reader.ReadToEnd();
                            HandleAccessTokenResponse(authToken);
                        }
            }
            catch (Exception ex)
            {
                logger.Error(ErrorMessages.AccessTokenRequestError(request.RequestUri.ToString(), new QueryParameters()), ex);
                throw new OAuthException(ErrorMessages.AccessTokenRequestError(request.RequestUri.ToString(), new QueryParameters()), ex);
            }
        }