private string GetAuthorizationHeader(WebPairCollection parameters)
        {
            var sb = new StringBuilder();
            if (!string.IsNullOrEmpty(Realm))
            {
                sb.Append(string.Format("realm=\"{0}\",", OAuthTools.UrlEncodeRelaxed(Realm)));
            }

            parameters.Sort((l, r) => string.Compare(l.Name, r.Name, StringComparison.Ordinal));
            var parameterCount = 0;
            var oathParameters = parameters.Where(
                parameter => !string.IsNullOrEmpty(parameter.Name)
                             && !string.IsNullOrEmpty(parameter.Value)
                             && (parameter.Name.StartsWith("oauth_") || parameter.Name.StartsWith("x_auth_"))).ToList();
            foreach (var parameter in oathParameters)
            {
                parameterCount++;
                var format = parameterCount < oathParameters.Count ? "{0}=\"{1}\"," : "{0}=\"{1}\"";
                sb.Append(string.Format(format, parameter.Name, parameter.Value));
            }

            var authorization = sb.ToString();
            return string.Format("{0} {1}", AuthenticationMethod, authorization);
        }
        private string GetAuthorizationHeader(WebPairCollection parameters)
        {
            var sb = new StringBuilder("OAuth ");
            if (!Realm.IsNullOrBlank())
            {
                sb.Append("realm=\"{0}\",".FormatWith(OAuthTools.UrlEncodeRelaxed(Realm)));
            }

            parameters.Sort((l, r) => l.Name.CompareTo(r.Name));

            var parameterCount = 0;
            var oathParameters = parameters.Where(parameter =>
                !parameter.Name.IsNullOrBlank() &&
                !parameter.Value.IsNullOrBlank() &&
                parameter.Name.StartsWith("oauth_")
                ).ToList();
            foreach (var parameter in oathParameters)
            {
                parameterCount++;
                var format = parameterCount < oathParameters.Count ? "{0}=\"{1}\"," : "{0}=\"{1}\"";
                sb.Append(format.FormatWith(parameter.Name, parameter.Value));
            }

            var authorization = sb.ToString();
            return authorization;
        }
        void AddOAuthData(IRestClient client, IRestRequest request, OAuthWorkflow workflow)
        {
            var requestUrl = client.BuildUriWithoutQueryParameters(request);

            if (requestUrl.Contains('?'))
            {
                throw new ApplicationException(
                          "Using query parameters in the base URL is not supported for OAuth calls. Consider using AddDefaultQueryParameter instead."
                          );
            }

            var url = client.BuildUri(request).ToString();
            var queryStringStart = url.IndexOf('?');

            if (queryStringStart != -1)
            {
                url = url.Substring(0, queryStringStart);
            }

            var method = request.Method.ToString().ToUpperInvariant();

            var parameters = new WebPairCollection();

            // include all GET and POST parameters before generating the signature
            // according to the RFC 5849 - The OAuth 1.0 Protocol
            // http://tools.ietf.org/html/rfc5849#section-3.4.1
            // if this change causes trouble we need to introduce a flag indicating the specific OAuth implementation level,
            // or implement a separate class for each OAuth version
            bool BaseQuery(Parameter x)
            => x.Type == ParameterType.GetOrPost || x.Type == ParameterType.QueryString || x.Type == ParameterType.QueryStringWithoutEncode;

            var query =
                request.AlwaysMultipartFormData || request.Files.Count > 0
                    ? x => BaseQuery(x) && x.Name.StartsWith("oauth_")
                    : (Func <Parameter, bool>)BaseQuery;

            parameters.AddRange(client.DefaultParameters.Where(query).ToWebParameters());
            parameters.AddRange(request.Parameters.Where(query).ToWebParameters());

            if (Type == OAuthType.RequestToken)
            {
                workflow.RequestTokenUrl = url;
            }
            else
            {
                workflow.AccessTokenUrl = url;
            }

            var oauth = Type switch
            {
                OAuthType.RequestToken => workflow.BuildRequestTokenInfo(method, parameters),
                OAuthType.AccessToken => workflow.BuildAccessTokenSignature(method, parameters),
                OAuthType.ClientAuthentication => workflow.BuildClientAuthAccessTokenSignature(method, parameters),
                OAuthType.ProtectedResource => workflow.BuildProtectedResourceSignature(method, parameters, url),
                _ => throw new ArgumentOutOfRangeException()
            };

            parameters.Add("oauth_signature", oauth);

            var oauthParameters = ParameterHandling switch
            {
                OAuthParameterHandling.HttpAuthorizationHeader => CreateHeaderParameters(),
                OAuthParameterHandling.UrlOrPostParameters => CreateUrlParameters(),
                _ =>
                throw new ArgumentOutOfRangeException()
            };

            request.AddOrUpdateParameters(oauthParameters);

            IEnumerable <Parameter> CreateHeaderParameters()
            => new[] { new Parameter("Authorization", GetAuthorizationHeader(parameters), ParameterType.HttpHeader) };

            IEnumerable <Parameter> CreateUrlParameters()
            => parameters.Where(p => !p.Name.IsNullOrBlank() && (p.Name.StartsWith("oauth_") || p.Name.StartsWith("x_auth_")))
            .Select(p => new Parameter(p.Name, HttpUtility.UrlDecode(p.Value), ParameterType.GetOrPost));
        }

        string GetAuthorizationHeader(WebPairCollection parameters)
        {
            var oathParameters =
                parameters
                .OrderBy(x => x, WebPair.Comparer)
                .Where(
                    p =>
                    !p.Name.IsNullOrBlank() && !p.Value.IsNullOrBlank() &&
                    (p.Name.StartsWith("oauth_") || p.Name.StartsWith("x_auth_"))
                    )
                .Select(x => $"{x.Name}=\"{x.Value}\"")
                .ToList();

            if (!Realm.IsNullOrBlank())
            {
                oathParameters.Insert(0, $"realm=\"{OAuthTools.UrlEncodeRelaxed(Realm)}\"");
            }

            return("OAuth " + string.Join(",", oathParameters));
        }
    }