예제 #1
0
        private string GenerateSignatureBase(Uri url, string consumerKey, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, SignatureTypes signatureType)
        {
            if (token == null)
            {
                token = string.Empty;
            }

            if (tokenSecret == null)
            {
                tokenSecret = string.Empty;
            }

            if (string.IsNullOrEmpty(consumerKey))
            {
                throw new ArgumentNullException("consumerKey");
            }

            if (string.IsNullOrEmpty(httpMethod))
            {
                throw new ArgumentNullException("httpMethod");
            }

            List <QueryParameter> parameters = GetQueryParameters(url.Query);

            if (IncludeVersion)
            {
                parameters.Add(new QueryParameter(OAuthVersionKey, OAuthVersion));
            }
            parameters.Add(new QueryParameter(OAuthNonceKey, nonce));
            parameters.Add(new QueryParameter(OAuthTimestampKey, timeStamp));
            parameters.Add(new QueryParameter(OAuthSignatureMethodKey, signatureType.GetDescription()));
            parameters.Add(new QueryParameter(OAuthConsumerKeyKey, consumerKey));

            if (!string.IsNullOrEmpty(token))
            {
                parameters.Add(new QueryParameter(OAuthTokenKey, token));
            }

            parameters.Sort(new QueryParameterComparer());

            var normalizedUrl = string.Format("{0}://{1}", url.Scheme, url.Host);

            if (!((url.Scheme == "http" && url.Port == 80) || (url.Scheme == "https" && url.Port == 443)))
            {
                normalizedUrl += ":" + url.Port;
            }
            normalizedUrl += url.AbsolutePath;
            var normalizedRequestParameters = NormalizeRequestParameters(parameters);

            StringBuilder signatureBase = new StringBuilder();

            signatureBase.AppendFormat("{0}&", httpMethod.ToUpper());
            signatureBase.AppendFormat("{0}&", Converter.UrlEncode(normalizedUrl));
            signatureBase.AppendFormat("{0}", Converter.UrlEncode(normalizedRequestParameters));

            return(signatureBase.ToString());
        }
예제 #2
0
        public Dictionary <string, string> GetParametersRequest(string url, string requestMethod)
        {
            Dictionary <string, string> retorno;

            Uri    requesturl             = new Uri(url);
            string TimeInSecondsSince1970 = ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
            string Nonce = Convert.ToBase64String(Encoding.UTF8.GetBytes(TimeInSecondsSince1970
                                                                         + TimeInSecondsSince1970 + TimeInSecondsSince1970));

            string signatureHash = GenerateSignature(requesturl, ConsumerKey, ConsumerSecretKey, TokenKey, TokenSecretKey, SignatureType, requestMethod.ToUpper(), TimeInSecondsSince1970, Nonce);

            retorno = new Dictionary <string, string> {
                { OAuthConsumerKeyKey, ConsumerKey },
                { OAuthNonceKey, Nonce },
                { OAuthTokenKey, TokenKey },
                { OAuthSignatureMethodKey, SignatureType.GetDescription() },
                { OAuthSignatureKey, signatureHash },
                { OAuthTimestampKey, TimeInSecondsSince1970 },
                { OAuthVersionKey, OAuthVersion }
            };

            return(retorno);
        }