Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public AuthorizeInfo GetAuthorizeInfo()
        {
            String             url     = this.RequestTokenUrl;
            var                cm      = new GetRequestTokenCommand(this.ConsumerKey, this.ConsumerSecret, "", "", HttpMethodName.Post);
            SignatureInfo      si      = OAuthClient.GenerateSignature(new Uri(url), cm, OAuthSignatureTypes.HMACSHA1);
            HttpClient         cl      = new HttpClient();
            HttpRequestCommand command =
                new HttpRequestCommand(String.Format("{0}?{1}&oauth_signature={2}", url, si.NormalizedRequestParameters, si.Signature), HttpMethodName.Post);
            String result = cl.GetBodyText(command);
            //正規表現でoauth_token,oauth_token_secret取得
            AuthorizeInfo ai = new AuthorizeInfo();

            ai.AuthorizeUrl       = String.Format("{0}?{1}", this.AuthorizeUrl, result);
            ai.RequestToken       = this.GetMatchValue(RegexList.OAuthToken, result);
            ai.RequestTokenSecret = this.GetMatchValue(RegexList.OAuthTokenSecret, result);
            return(ai);
        }
Пример #2
0
        private HttpRequestCommand CreateHttpClientQueryStringMode(HttpMethodName methodName, String url, String token, String tokenSecret, IDictionary <String, String> queryString)
        {
            var cm = new GetRequestTokenCommand(this.ConsumerKey, this.ConsumerSecret, token, tokenSecret, methodName);
            Dictionary <String, String> pp = OAuth1Client.GenerateParameters(cm);

            foreach (var p in queryString)
            {
                pp.Add(p.Key, p.Value);
            }
            var           u  = new Uri(HttpClient.CreateQueryString(url, pp, OAuth1Client.UrlEncode));
            SignatureInfo si = GenerateSignature(u, cm);

            pp.Add("oauth_signature", OAuth1Client.UrlEncode(si.Signature));
            HttpRequestCommand cl = new HttpRequestCommand(HttpClient.CreateQueryString(url, pp, HttpClient.UrlEncode));

            cl.MethodName = methodName;
            return(cl);
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <param name="command"></param>
        /// <param name="signatureType"></param>
        /// <returns></returns>
        public static SignatureInfo GenerateSignatureBase(Uri url, GetRequestTokenCommand command, String signatureType)
        {
            SignatureInfo si = new SignatureInfo();
            var           cm = command;

            if (String.IsNullOrEmpty(signatureType))
            {
                throw new ArgumentNullException("signatureType");
            }

            List <KeyValuePair <String, String> > parameters = OAuth1Client.GetQueryParameters(url.Query);

            parameters.Add(new KeyValuePair <String, String>(Key.OAuthVersion, Key.OAuthVersionNo));
            parameters.Add(new KeyValuePair <String, String>(Key.OAuthNonce, cm.Nonce));
            parameters.Add(new KeyValuePair <String, String>(Key.OAuthTimestamp, cm.TimeStamp));
            parameters.Add(new KeyValuePair <String, String>(Key.OAuthSignatureMethod, signatureType));
            parameters.Add(new KeyValuePair <String, String>(Key.OAuthConsumerKey, cm.ConsumerKey));

            if (!String.IsNullOrEmpty(cm.Token))
            {
                parameters.Add(new KeyValuePair <String, String>(Key.OAuthToken, cm.Token));
            }

            parameters.Sort(CompareQueryString);

            si.NormalizedUrl = String.Format("{0}://{1}", url.Scheme, url.Host);
            if (!((url.Scheme == "http" && url.Port == 80) || (url.Scheme == "https" && url.Port == 443)))
            {
                si.NormalizedUrl += ":" + url.Port;
            }
            si.NormalizedUrl += url.AbsolutePath;
            si.NormalizedRequestParameters = NormalizeRequestParameters(parameters);

            StringBuilder sb = new StringBuilder(1000);

            sb.AppendFormat("{0}&", cm.MethodName.ToString().ToUpper());
            sb.AppendFormat("{0}&", OAuth1Client.UrlEncode(si.NormalizedUrl));
            sb.AppendFormat("{0}", OAuth1Client.UrlEncode(si.NormalizedRequestParameters));
            si.Signature = sb.ToString();
            return(si);
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <param name="command"></param>
        /// <param name="signatureType"></param>
        /// <returns></returns>
        public static SignatureInfo GenerateSignature(Uri url, GetRequestTokenCommand command, OAuthSignatureTypes signatureType)
        {
            var           cm = command;
            SignatureInfo si = new SignatureInfo();

            switch (signatureType)
            {
            case OAuthSignatureTypes.PLAINTEXT:
                si.Signature = OAuth1Client.UrlEncode(String.Format("{0}&{1}", cm.ConsumerKeySecret, cm.TokenSecret));
                return(si);

            case OAuthSignatureTypes.HMACSHA1:
                si = GenerateSignatureBase(url, cm, Key.HMACSHA1SignatureType);
                String key = String.Format("{0}&{1}"
                                           , OAuth1Client.UrlEncode(cm.ConsumerKeySecret), String.IsNullOrEmpty(cm.TokenSecret) ? "" : OAuth1Client.UrlEncode(cm.TokenSecret));
                si.Signature = GenerateSignatureUsingHash(key, si.Signature);
                return(si);

            case OAuthSignatureTypes.RSASHA1: throw new NotImplementedException();
            }
            throw new ArgumentException("Unknown signature type", "signatureType");
        }